public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* other/5788: GCC compiled programs treat double quotes in command line arguments incorrectly
@ 2002-02-26 13:16 michael
  0 siblings, 0 replies; 2+ messages in thread
From: michael @ 2002-02-26 13:16 UTC (permalink / raw)
  To: gcc-gnats


>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:


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: other/5788: GCC compiled programs treat double quotes in command line arguments incorrectly
@ 2002-03-31  2:22 rth
  0 siblings, 0 replies; 2+ messages in thread
From: rth @ 2002-03-31  2:22 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, michael, nobody

Synopsis: GCC compiled programs treat double quotes in command line arguments incorrectly

State-Changed-From-To: open->closed
State-Changed-By: rth
State-Changed-When: Sun Mar 31 02:22:38 2002
State-Changed-Why:
    Not a gcc issue.  Contact the cygwin folks about this.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5788


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-03-31 10:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-26 13:16 other/5788: GCC compiled programs treat double quotes in command line arguments incorrectly michael
2002-03-31  2:22 rth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).