Repository navigation
fix: escape backslashes when quoting process arguments - #2797
Open
drewster99 wants to merge 2 commits into
Open
drewster99 wants to merge 2 commits into
drewster99 wants to merge 2 commits into
Conversation
StringExtensions.Quoted() wrapped a value in double quotes and escaped
embedded quotes, but left backslashes untouched. The result is assigned
to ProcessStartInfo.Arguments, which .NET re-parses into argv using
MSVC-style rules on every platform, where a backslash before a double
quote is significant. A value ending in a backslash, or containing a
backslash immediately before a quote, was therefore handed to git as a
different string.
In practice a file whose name ends in a backslash (valid on macOS and
Linux) reached commands such as `show {rev}:{file}` and
`cat-file -s {rev}:{file}` as the wrong path, so viewing or saving that
file from history operated on the wrong target or failed.
Apply the same escaping .NET itself uses (System.PasteArguments) so the
value round-trips verbatim. Add tools/quote-roundtrip-test, which checks
the invariant against .NET's real argument parser.
Quoted() no longer delegates to Escaped(), and nothing else references it, so drop the dead method. EscapeForBRE() is unrelated and retained.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2796.
Problem
StringExtensions.Quoted()wraps a value in double quotes and escapes embedded", but leaves backslashes untouched. The result is assigned toProcessStartInfo.Arguments, which .NET re-parses intoargvusing MSVC-style rules on every platform, where a backslash before a double quote is significant. A value ending in a backslash, or containing a backslash immediately before a quote, is therefore handed togitas a different string.In practice a file whose name ends in a backslash (valid on macOS and Linux) reaches commands such as
show {rev}:{file}andcat-file -s {rev}:{file}as the wrong path, so viewing or saving that file from history operates on the wrong target or fails.Fix
Escape the value the same way .NET itself does when building an argument (
System.PasteArguments): double any run of backslashes that precedes a quote or the closing quote, and escape embedded quotes. Because everygitcall funnels throughQuoted()intoArgs, this one change covers all call sites.Test
Adds
tools/quote-roundtrip-test, a standalone console check that asserts the invariant "Quoted(name)parses back into exactly oneargventry equal toname" against .NET's real argument parser, using only benign filenames (no git, no payload). It fails on the old implementation and passes on the new one.Verification
dotnet build -c Debug src/SourceGit.csproj— succeeds, no new warnings.dotnet format --verify-no-changes src/SourceGit.csproj— clean.dotnet run -c Releaseintools/quote-roundtrip-test— all inputs pass.