Skip to content

Commit fd55745

Browse files
cliffhallclaude
andcommitted
fix(scripts): double backslashes before a quote in win-shell-args
An argument ending in a backslash was quoted as "...\\", which the CRT parser reads as an escaped quote, swallowing the rest of the command line. Backslash runs before an embedded quote or the closing quote are now doubled, with tests including a round trip through a reference CRT parser. Closes #5111 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent d7f76a6 commit fd55745

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎scripts/lib/win-shell-args.mjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ export function quoteWinArg(arg) {
4343
);
4444
}
4545
if (!NEEDS_QUOTING.test(arg)) return arg;
46-
// A literal `"` inside a quoted string is doubled, per the CRT parser.
47-
return `"${arg.replace(/"/g, '""')}"`;
46+
// The CRT parser reads backslashes literally except before a `"`, where 2n
47+
// backslashes yield n and leave the quote special. So any run of backslashes
48+
// that ends up in front of a quote is doubled: one before an embedded `"`,
49+
// and a trailing run, which the closing quote follows (#5111). Undoubled,
50+
// `C:\dir\` becomes `"C:\dir\"`, whose `\"` is an escaped quote and swallows
51+
// the rest of the command line. The embedded `"` itself is then doubled
52+
// rather than written `\"`: `""` keeps `cmd.exe`'s own quote tracking in
53+
// step, while `\"` would flip it and expose any metacharacter after it.
54+
const escaped = arg.replace(/(\\*)"/g, '$1$1""').replace(/(\\+)$/, "$1$1");
55+
return `"${escaped}"`;
4856
}
4957

5058
/**

‎scripts/lib/win-shell-args.test.mjs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,68 @@ test("doubles an embedded quote", () => {
3939
assert.equal(quoteWinArg('a"b c'), '"a""b c"');
4040
});
4141

42+
test("doubles a trailing backslash run, which the closing quote follows (#5111)", () => {
43+
// Undoubled, `"C:\dir with space\"` ends in `\"`, an escaped quote, and the
44+
// CRT parser runs the argument on into the next one.
45+
assert.equal(quoteWinArg("C:\\a b\\"), '"C:\\a b\\\\"');
46+
assert.equal(quoteWinArg("C:\\a b\\\\"), '"C:\\a b\\\\\\\\"');
47+
});
48+
49+
test("doubles backslashes before an embedded quote, then doubles the quote", () => {
50+
assert.equal(quoteWinArg('a\\"b c'), '"a\\\\""b c"');
51+
assert.equal(quoteWinArg('a\\\\"b'), '"a\\\\\\\\""b"');
52+
});
53+
54+
test("leaves backslashes not followed by a quote alone", () => {
55+
assert.equal(quoteWinArg("C:\\a b\\c"), '"C:\\a b\\c"');
56+
// An inert trailing-backslash path is not quoted, so nothing follows it.
57+
assert.equal(quoteWinArg("C:\\dir\\"), "C:\\dir\\");
58+
});
59+
60+
test("quoted output parses back to the original under the CRT rules", () => {
61+
// A reference parser for one quoted argument (the MSVCRT 2008+ rules), so
62+
// the expectations above are checked against what the child actually sees.
63+
const parse = (s) => {
64+
let out = "";
65+
let inQuotes = false;
66+
for (let i = 0; i < s.length; ) {
67+
let n = 0;
68+
while (s[i] === "\\") {
69+
n++;
70+
i++;
71+
}
72+
if (s[i] === '"') {
73+
out += "\\".repeat(n >> 1);
74+
if (n & 1) {
75+
out += '"';
76+
} else if (inQuotes && s[i + 1] === '"') {
77+
out += '"';
78+
i++;
79+
} else {
80+
inQuotes = !inQuotes;
81+
}
82+
i++;
83+
} else {
84+
out += "\\".repeat(n);
85+
if (i < s.length) out += s[i++];
86+
}
87+
}
88+
return out;
89+
};
90+
for (const arg of [
91+
"C:\\a b\\",
92+
"C:\\a b\\\\",
93+
'a\\"b c',
94+
'a\\\\"b',
95+
'a"b c',
96+
'"',
97+
"\\ \\",
98+
"C:\\Temp(1)\\",
99+
]) {
100+
assert.equal(parse(quoteWinArg(arg)), arg, `round trip: ${arg}`);
101+
}
102+
});
103+
42104
test("throws on `%` rather than silently mis-expanding it", () => {
43105
// cmd.exe expands %NAME% before it processes quotes, so quoting cannot save
44106
// this one — the child would receive a different path than we passed.

0 commit comments

Comments
 (0)