Skip to content

Fix Windows argument quoting for backslash runs before quotes - #101

Open
ServeurpersoCom wants to merge 2 commits into
sheredom:mainfrom
ServeurpersoCom:fix/windows-argument-quoting
Open

Fix Windows argument quoting for backslash runs before quotes#101
ServeurpersoCom wants to merge 2 commits into
sheredom:mainfrom
ServeurpersoCom:fix/windows-argument-quoting

Conversation

@ServeurpersoCom

Copy link
Copy Markdown

As explained in #100, we hit this in llama.cpp where subprocess.h launches MCP servers on Windows: an argument like C:\my dir\ reaches the child merged with everything after it, because the trailing backslash escapes the closing quote the builder adds.

Proposed fix: count backslash runs and double the whole run when it lands before a quote, including the generated closing quote, as the CRT parsing rules expect.

The extended special argv test fails on Windows without the fix and passes with it; on POSIX argv is passed as-is. Tested on a dedicated physical Windows 11 machine (before/after screenshot in #100).

Fixes #100

The command line builder doubles a backslash only when the next source
character is a quote. Per the CRT parsing rules, every backslash in a
run preceding a double quote must be doubled, and this includes a run
that ends up before the generated closing quote of a quoted argument.

As a result, a quoted argument ending in a backslash swallows its
closing quote and merges with every following argument, a run of two
or more backslashes before an embedded quote is under-escaped, and a
quoted argument ending in several backslashes loses half of them.

Count backslash runs in the length and emit passes and double the
whole run before an escaped quote or before the closing quote. Extend
the special argv test with the three cases plus a merge canary.
Rework the two command line builder passes without changing the
emitted bytes. The length pass becomes a linear scan that carries
the current backslash run, and the emit pass folds the duplicated
write loops into the explicit 2n + 1 and 2n CRT formulas.

Extend the special argv test with a quoted argument combining a
space and a backslash run before an embedded quote.
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.

Windows: an argument ending in a backslash breaks the command line

1 participant