os: commandLineToArgv() does not correctly parse argv[0] in Windows #50028
Labels
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
OS-Windows
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
When running a go program in Windows cmd with a path such as this:
os.Args
will contain two arguments:C:"Program
andFiles\main.exe
.Here is an example that uses the definition of
commandLineToArgv()
fromos/exec_windows.go
: https://go.dev/play/p/VgBbsDdApUgWhat did you expect to see?
os.Args
should contain a single arg:C:\Program Files\main.exe
.This is not happening because we are not giving special treatment to the first argument. As specified in the documentation for Parsing C++ command-line arguments:
In this case, (
\"
) is interpreted as a literal double quote mark ("
) and the space inProgram Files
is interpreted as the end of an argument. This is incorrect for the first argument.David Deley's command line parsing reference also corroborates that argument 0 should be parsed specially. (Although admittedly it's not very clear - the flowchart skips this step for example.)
As far as I can see, when we implemented
commandLineToArgv()
in #15588, we used the C++ documentation and David Deley's page as reference. However, we did not correctly implement the algorithm specified in these references. So I believe this behavior is unintended. This problem has existed since we fixed #15588.What did you see instead?
os.Args
contains two arguments since the first arg is not parsed correctly:C:"Program
andFiles\main.exe
Please let me know if my diagnosis is correct. If so, I would be happy to submit a patch for this.
The text was updated successfully, but these errors were encountered: