Skip to content

launcher re-quotes argv unconditionally: args with double quotes or trailing backslashes are corrupted (messages and path args alike) #4

Closed

Description

@caomengxuan666

Summary

scripts/entry/launch-git.c rebuilds the child command line with
quote_append(), which wraps every argument in literal double quotes
without escaping:

static void quote_append(WCHAR *dst, size_t cap, const WCHAR *arg)
{
    ...
    _snwprintf(dst, cap, L"\"%s\" ", arg);
}

The child git.exe parses that line with the standard CRT rules, so any
argument that itself contains " is mangled, and any argument ending in \
has that backslash reinterpret the closing quote (\" = escaped quote).
Both corrupt the argument. Severity: high — this hits everyday Windows
path arguments (trailing backslash after a directory name) as well as commit
messages that quote shell snippets, and it happens silently (rc=0).

Minimal repro (differential, wrapper vs real mingw64/bin/git.exe)

All runs feed argv directly (subprocess list form, no shell involved).

1. commit message with embedded quotes — quotes silently stripped

$ wrapper git commit -m 'she said "hi" and left'      # message stored: she said hi and left
$ real    git commit -m 'she said "hi" and left'      # message stored: she said "hi" and left

2. commit message ending with a backslash — backslash becomes a quote

$ wrapper git commit --amend -m 'ends with backslash \'
  -> stored message: ends with backslash "          (rc=0, silent)
$ real git commit --amend -m 'ends with backslash \'
  -> stored message: ends with backslash \

If the mangled argument is not the last one, the unbalanced quote swallows
the following arguments: git commit -m 'ends with backslash \' --allow-empty
in an empty repo fails with "nothing to commit" because --allow-empty gets
absorbed into the -m value.

3. directory path with trailing backslash — hard failure

$ wrapper git add 'D:\repo\project\sub\'
  -> fatal: pathspec 'D:\repo\project\sub" ' did not match any files   (rc=128)
$ real git add 'D:\repo\project\sub\'
  -> (rc=0, adds the directory)

The \" eats the closing quote and the separator space leaks into the next
argument.

4. path containing a quote — quote stripped

$ wrapper git add 'we"ird.txt'   -> pathspec 'weird.txt ' (mangled)
$ real    git add 'we"ird.txt'   -> pathspec 'we"ird.txt'

Mechanism

argv[i] = "she said \"hi\" and left" is emitted as "she said "hi" and left".
CRT parsing: opens quote, she said , closes at the inner ", then re-opens…
result: she said hi and left. For the trailing-backslash case the closing
quote is escaped (\") so the argument never terminates where it should.

Fix direction

Emit standard Windows argv quoting (quote only when the arg is empty or
contains space/tab/quote; inside quotes double backslashes that precede a
quote, emit " as \", and double trailing backslashes before the closing
quote). Also size the command-line buffer from actual content (current fixed
32 KiB can truncate/overflow under the same paths).

Verification

Differential harness (same command through wrapper and real git in twin
repos, byte-compare HEAD message/reflog/stdout/stderr/rc) — happy to
contribute it plus a regression test for these cases as part of the
release-blocking fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions