Skip to content

Conversation

@SteveL-MSFT
Copy link
Member

When we create the arg string, an arg of .\test 1\ ends up being ".\test 1\" which is stored in StartInfo.Arguments which then corefx passes as-is to SHELLEXECUTEINFO.

The native command receives the arg as .\test 1" as the last \" is treated as escaping the quotes. Fix is to add an extra backslash to escape the last enclosing quote.

Fix #4358

escape last backslash before adding quote
@PetSerAl
Copy link
Contributor

PetSerAl commented Oct 1, 2017

Since not every application use \ as escapement it will also affect behavior of that programs. For example:

cmd /c 'echo \'

will print \\ instead of \.

Also you should not only double one \ at the end, but whole sequence of \ in the end. For example:

cmd /c 'echo \\\'

will print \\\\, while it should be \\\\\\ to be properly recognized as three literal \.

}

It "Should correctly quote paths with spaces" {
$lines = testexe -echoargs '.\test 1\' '.\test 2\'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check double quotes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add test

@SteveL-MSFT
Copy link
Member Author

@PetSerAl for cmd echo, perhaps the only thing we can do is suggest using cmd --% /c echo \

modify test to also use double quotes
@PetSerAl
Copy link
Contributor

PetSerAl commented Oct 1, 2017

How about multiple trailing \?:

testexe -echoargs '.\test 1\\' ".\test 2\\"

@SteveL-MSFT
Copy link
Member Author

@PetSerAl I'll work on that and add a test

support multiple trailing backslashes
@SteveL-MSFT
Copy link
Member Author

Apparently these strange trailing backslash and double quotes rules are documented

// just the normal double quotes, no other special quotes. Also note that mismatched
// quotes are supported
if (NeedQuotes(arg))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

_arguments.Append('"'); 
_arguments.Append(arg); 
for (int i = arg.Length-1; i >= 0 && arg[i] == '\\'; i--)
{
    _arguments.Append('\\');
}
_arguments.Append('"'); 

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's much better than my attempt. Will change.

address PR feedback
for (int i = arg.Length-1; i >= 0 && arg[i] == '\\'; i--)
{
_arguments.Append(arg);
_arguments.Append('\\');
Copy link
Collaborator

@iSazonov iSazonov Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be "\\\\"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, unless you want to triplicate trailing \. arg already have one copy of \. This just adding second copy on top of it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarify!

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 6, 2017

@daxian-dbw @lzybkr Could you please review?

1 similar comment
@iSazonov
Copy link
Collaborator

@daxian-dbw @lzybkr Could you please review?

@SteveL-MSFT SteveL-MSFT requested a review from anmenaga October 10, 2017 16:09
@SteveL-MSFT
Copy link
Member Author

@daxian-dbw is in training today, @anmenaga can you review?

@daxian-dbw
Copy link
Member

Sorry that I missed the the first "@mention" notification. I will take a look later today.
I wish @mklement0 can review this if possible, as he has a better understanding of the native argument parsing user experience in PowerShell.

Copy link

@anmenaga anmenaga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@iSazonov iSazonov merged commit 59311d0 into PowerShell:master Oct 11, 2017
@SteveL-MSFT SteveL-MSFT deleted the trailing-slash branch October 26, 2018 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants