other/5788: GCC compiled programs treat double quotes in command line arguments incorrectly

michael@michael.be michael@michael.be
Tue Feb 26 13:48:00 GMT 2002


>Number:         5788
>Category:       other
>Synopsis:       GCC compiled programs treat double quotes in command line arguments incorrectly
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Tue Feb 26 13:06:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Michael Korntheuer
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
Windows 2000, 95, 98 and probably other
>Description:

 Hi,

There seems to be a problem with programs compiled by gcc
under Windows. They do not treat double quotes in their
command line arguments as they should. I have noticed this
problem in my own programs but also in other programs known to
be compiled with gcc. I have tested for this bug under Windows
95/98 and 2000, and I think this bug is quiet general in 
several versions of gcc under several versions of Windows.

The bug is the following:
Double quotes are treated as they should except for
argument 0 ( the name of program, as it was called ).
Usually nobody bothers about argument 0, but in some
cases it is used by the program to know its location
which may be used to access resource files. A double
quote in argument 0 may split it up and treat the second
half as argument 1 ( and thus shift all the other arguments
one place to the right ). At the command line this gives:

"prog"ram.exe arg1 arg2

becomes

prog ram.exe arg1 arg2

The program.exe gets started because the DOS shell does
the double quote interpretation without error, but the
program gets 3 arguments ie. 'ram.exe', 'arg1' and 'arg2'.
This is obviously wrong and it's rather hard to
program a work-a-round for this bug.

You may ask yourself who would call a program in such
a strange way, patially quoted. You'll probably never
do this at the command line, but it happens a lot in
script files ( batch files ).

As an example look at this session at the DOS prompt
( Windows 2000 service pack 2, gcc 2.95.2 )



C:\Program Files\Test>type args.c
#include <stdio.h>

main (int argc, char *argv[]){
        printf("(%i) <%s> <%s> <%s>\n",argc,argv[0],argv[1],argv[2]);
}
C:\Program Files\Test>gcc args.c

C:\Program Files\Test>a.exe arg1 arg2
(3) <a.exe> <arg1> <arg2>

C:\Program Files\Test>a.exe "  "arg1with" spaces" " arg2 "with" "spaces
(3) <a.exe> <  arg1with spaces> < arg2 with spaces>

C:\Program Files\Test>"C:\Program Files\Test\a.exe" arg1 arg2
(3) <C:\Program Files\Test\a.exe> <arg1> <arg2>

C:\Program Files\Test>"C:\Program Files\Test\"a.exe arg1 arg2
(4) <C:\Program Files\Test\> <a.exe> <arg1>

C:\Program Files\Test>type batch.bat
%0\..\a.exe arg1 arg2

C:\Program Files\Test>batch.bat

C:\Program Files\Test>batch.bat\..\a.exe arg1 arg2
(3) <batch.bat\..\a.exe> <arg1> <arg2>

C:\Program Files\Test>cd \

C:\>"C:\Program Files\Test\batch.bat"

C:\>"C:\Program Files\Test\batch.bat"\..\a.exe arg1 arg2
(4) <C:\Program Files\Test\batch.bat> <\..\a.exe> <arg1>

C:\>



Script files have a .bat extension under Windows and
contrary to Unix every line in the script file is echoed
at the command line ( after substitution of variables ) 
before it is executed. The variable %0 is argument 0 of
the batch file, ie. the name how it was called.

I guess this bug can easily be repaired since double quotes
in the other arguments seem to be interpreted correctly.

>How-To-Repeat:
#include <stdio.h>

main (int argc, char *argv[]){
	printf("(%i) <%s> <%s> <%s>\n",argc,argv[0],argv[1],argv[2]);
}
>Fix:
Test argument 1 for a '\' as first character. If it is
a '\' you will have to concat argument 0 and argument 1 and
put it in argument 0. You will also have to shift the other arguments one place to the left ( argument 2 becomes argument 1, argument 3 becomes argument 2 etc... ).
This is not a fail save fix...
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list