From fb26be33a11999e0ba6a0dfde0cc271890792fa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Wed, 10 Oct 2018 23:31:52 +0300 Subject: [PATCH] Correct the escaping of arguments passed through on windows --- wrappers/clang-target-wrapper.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/wrappers/clang-target-wrapper.c b/wrappers/clang-target-wrapper.c index 7097786..d432c07 100644 --- a/wrappers/clang-target-wrapper.c +++ b/wrappers/clang-target-wrapper.c @@ -53,10 +53,22 @@ static TCHAR *escape(const TCHAR *str) { int i; *ptr++ = '"'; for (i = 0; str[i]; i++) { - if (str[i] == '"') + if (str[i] == '"') { + int j = i - 1; + // Before all double quotes, backslashes need to be escaped, but + // not elsewhere. + while (j >= 0 && str[j--] == '\\') + *ptr++ = '\\'; + // Escape the next double quote. *ptr++ = '\\'; + } *ptr++ = str[i]; } + // Any final backslashes, before the quote around the whole argument, + // need to be doubled. + int j = i - 1; + while (j >= 0 && str[j--] == '\\') + *ptr++ = '\\'; *ptr++ = '"'; *ptr++ = '\0'; return out;