Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In Windows, Trailing Backslash In Quoted Path Argument Leads to Wrong Command-line Parsing #72653

Closed
git-blame opened this issue May 27, 2020 · 4 comments
Labels
O-windows Operating system: Windows

Comments

@git-blame
Copy link

If a rust windows program has a command-line argument like this: .\myapp -p "C:\Program Files\" -v, it will be parsed as follows: ['.\myapp', '-p', 'C:\\Program Files\\" -v'] instead of ['.\myapp', '-p', 'C:\\Program Files\\', '-v'].

Looking over rust code, this is due to rust adhering to Microsoft standard:

/// Implements the Windows command-line argument parsing algorithm.
///
/// Microsoft's documentation for the Windows CLI argument format can be found at
/// <https://docs.microsoft.com/en-us/previous-versions//17w5ykft(v=vs.85)>.

And the link states:

A double quotation mark preceded by a backslash (") is interpreted as a literal double quotation mark character (").

So everything is behaving to spec. The problem is that if a user is running my app in powershell, tab-completion will automatically add in the trailing backslash in quoted paths. So I have to explain that this doesn't work.

Some observations:

  • Tab-completion in command prompt (cmd) does not add trailing backslash
  • Microsoft commands like dir, ls don't have a problem with trailing slash (in powershell or cmd). For example dir "C:\Program Files\" /B
  • I tried using Windows function CommandLineToArgvW through the winapi crate but it also seems to have the same problem.
@jonas-schievink
Copy link
Contributor

This sounds like Rust is behaving correctly though? Isn't this a bug in Powershell?

@retep998
Copy link
Member

Given that Rust's behavior matches CommandLineToArgvW, I'm going to say Rust is behaving as expected and the bug is on powershell's side.

@retep998 retep998 added the O-windows Operating system: Windows label May 28, 2020
@jonas-schievink
Copy link
Contributor

Closing as expected behavior.

@git-blame
Copy link
Author

There are numerous problems with powershell quoting that is exarcebated with backslash. One of them is that ps will remove quotes if there are no spaces in the argument as detailed here: PowerShell/PowerShell#11295 (comment)

  • calling with -p "a\\" -v, your binary receives the command-line string <execname> -p a\\ -v
  • but calling with -p "a \\" -v, your binary receives <execname> -p "a \\" -v

So if you are depending on Rust's built-in windows command-line parser, your C/C++ compiled program's argc, argv arguments, or using explicitly functions like CommandLineToArgvW, you will get different results. Notably, without the double-quotes in the first example, it will not convert the pairs of backslash into a single backslash.

Command-prompt (cmd) passes the command-line string as is and so you get a consistent (ie, predictable) parsing of your arguments. So you may want to run your rust binary using cmd on Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows
Projects
None yet
Development

No branches or pull requests

3 participants