Bug 42996 - Incorrect length returned from get_command_argument intrinsic
Summary: Incorrect length returned from get_command_argument intrinsic
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-02-08 10:44 UTC by Ian Harvey
Modified: 2010-02-09 17:07 UTC (History)
1 user (show)

See Also:
Host: i686-pc-mingw32
Target: i686-pc-mingw32
Build: i686-pc-mingw32
Known to work:
Known to fail:
Last reconfirmed: 2010-02-08 13:19:24


Attachments
Simple test case (329 bytes, text/plain)
2010-02-08 10:48 UTC, Ian Harvey
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Harvey 2010-02-08 10:44:27 UTC
According to the F2003 standard, the LENGTH argument does "not consider any possible truncation or padding in assigning the command argument value to the VALUE argument" (13.7.42).  However, gfortran appears to use the minimum of the length of the VALUE argument (if present) and the length of the command argument.

(svn revision 156557)
Comment 1 Ian Harvey 2010-02-08 10:48:45 UTC
Created attachment 19819 [details]
Simple test case

Call the resulting program with an argument longer than one character.
Comment 2 Tobias Burnus 2010-02-08 13:19:24 UTC
Confirm - I get for "./a.out test" the following result.

Current (gfortran):
   With no value, length was:4 and status:0
   With LEN=1 value, length was:1 and status:-1 <<<<< Bad: len = 1
   With LEN=10 value, length was:4 and status:0

Expected (NAG f95, ifort):
   With no value, length was:4 and status:0
   With LEN=1 value, length was:4 and status:-1 <<<<< OK: len = 4
   With LEN=10 value, length was:4 and status:0

By the way, the manual is correct:
http://gcc.gnu.org/onlinedocs/gfortran/GET_005fCOMMAND_005fARGUMENT.html


Draft patch:

diff --git a/libgfortran/intrinsics/args.c b/libgfortran/intrinsics/args.c
index 7187bec..83a0502 100644
--- a/libgfortran/intrinsics/args.c
+++ b/libgfortran/intrinsics/args.c
@@ -147,11 +147,9 @@ get_command_argument_i4 (GFC_INTEGER_4 *number, char *value,
   if (value != NULL && stat_flag != GFC_GC_FAILURE)
     {
       if (arglen > value_len)
-       {
-        arglen = value_len;
         stat_flag = GFC_GC_VALUE_TOO_SHORT;
-       }
-      memcpy (value, argv[*number], arglen);
+
+      memcpy (value, argv[*number], arglen > value_len ? value_len : arglen);
     }

   if (length != NULL)
Comment 3 Tobias Burnus 2010-02-09 17:05:17 UTC
Subject: Bug 42996

Author: burnus
Date: Tue Feb  9 17:04:57 2010
New Revision: 156630

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=156630
Log:
2010-02-09  Tobias Burnus  <burnus@net-b.de>

        PR fortran/42996
        * intrinsics/args.c (get_command_argument_i4): Always return
        commandline-argument length for length parameter.


Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/intrinsics/args.c

Comment 4 Tobias Burnus 2010-02-09 17:07:15 UTC
FIXED on the trunk (4.5).

Thanks for the bug report!