Command line processing in dcrt0.cc does not match Microsoft parsing rules

Eric Blake eblake@redhat.com
Thu Sep 5 22:46:00 GMT 2019


On 9/5/19 5:01 PM, Stephen Provine via cygwin wrote:
> On 9/5/19 2:05 PM, Eric Blake wrote:
>> On 9/5/19 1:31 PM, Stephen Provine via cygwin wrote:
>>> Not expected.
> 
>> Why not? That obeyed cmd's odd rules: The moment you have a " in the
>> command line, that argument continues until end of line or the next "
>> (regardless of how many \ precede the ").
> 
> Now I'm really confused. Brian seemed to indicate that the POSIX rules were
> followed, but you're indicating that the Windows command line parsing rules
> are followed. So I assume the reality is that it is actually some mix of the two.
> Is the effective parsing logic implemented by Cygwin documented anywhere?

If you start a Cygwin process from another cygwin process, then only
POSIX rules are in effect.  The bash shell parses its command line
according to POSIX rules, creates an argv[] to pass to exec(), then
cygwin1.dll manages to get that argv[], unscathed, to the new child
process (bypassing Window's mechanisms), which uses the argv[] as-is.

If you start a Windows process from a cygwin process, then cygwin1.dll
must quote the arguments into a single concatenated string that will be
reparsed in the manner that the Windows runtime expects, because the
Windows process only gets a single string, not an argv[].  But cygwin
should be providing the correct escaping so that windows then parses it
back into the same intended argv[] (if not, that's a bug in cygwin1.dll).

If you start a cygwin process from Windows, then cygwin1.dll is given
only a single string, which it must parse into argv according to windows
conventions (if it does not produce the same argv[] as a windows process
using CommandLineToArgvW, then that's a bug in cygwin1.dll).  But on top
of that, if you are using cmd.exe to generate your command line, then
you must use proper escaping, otherwise, cmd.exe can produce a command
line that has unexpected quoting in the string handed to
CommandLineToArgvW, and the Windows parsing when there are unbalanced
quotes can be screwy (if it encounters a " inside an argument that was
not quoted with ", then that groups remaining text into the same
argument until a balanced " or end of string is encountered).  So it is
not always obvious at first glance if what you type in cmd.exe provides
the argv[] that you intended, because of the two layers of
interpretation (one from cmd to Windows, and one from Windows convention
into argv[]).

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://cygwin.com/pipermail/cygwin/attachments/20190905/9baf2592/attachment.sig>


More information about the Cygwin mailing list