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

Improve command line argument quoting logic #335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Improve command line argument quoting logic #335

wants to merge 2 commits into from

Conversation

nahk-ivanov
Copy link

We hit an issue where C# generator tool would exit with no error message and no files generated.

After troubleshooting I root-caused the issue to .NET command line parsing rules, where special characters like double-quotes and backslashes have to be escaped under some circumstances.

There already was a code to handle some of these cases, but it didn't cover the following:

  1. When there are multiple backslashes preceding the double-quote, we need to escape all of them, not only the last one.
  2. When there is a backslash at the end of the argument, we need to escape it, otherwise it will escape the double-quote appended by the code itself, causing attribute value to run out when parsed by .NET infrastructure.

This change is improving the logic to account for these two extra cases. I also added unit tests to demonstrate the behavior. In order to do so, I had to move the JoinArguments method to a public helper class, so that it can be accessed from unit tests, as I'm not a big fan of InternalsVisibleTo.

…age and no files generated.

After troubleshooting I root-caused the issue to .NET command line parsing rules, where special characters like double-quotes and backslashes have to be escaped under some circumstances.

There already was a code to handle some of these cases, but it didn't cover the following:
1) When there are multiple backslashes preceeding the double-quote, we need to escape all of them, not only the last one.
2) When there is a backslash at the end of the argument, we need to escape it, otherwise it will escape the double-quote appended by the code itself, causing attribute value to run out when parsed by .NET infrastructure.

This change is improving the logic to account for these two extra cases. I also added unit tests to demonstrate the behavior. In order to do so, I had to move the JoinArguments methd to a public helper class, so that it can be accessed from unit tests, as I'm not a big fan of InternalsVisibleTo.
}

// escape backslashes appearing before a double quote or end of line by doubling them
string arg = Regex.Replace(argument, @"(\\+)(""|$)", "$1$1$2");
Copy link
Author

Choose a reason for hiding this comment

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

If you don't want tests/new helper class, then all it takes to fix it is to replace this line in the original method.

@nahk-ivanov
Copy link
Author

I think this is related to #320 and PR #332.

@sharwell
Copy link
Member

sharwell commented Aug 7, 2019

I don't mind adding a new class and test project, but we definitely want IVT to the test project and change the helper class to internal. Misuse of IVTs causes a maintenance burden on many projects, but the one place they excel is clean testing of internal (non-private) APIs.

@nahk-ivanov
Copy link
Author

Done. Luckily there was already a signing key for test assemblies, so I didn't have to add a new one.

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.

2 participants