From 99b6407196e5b4e61bcb4d7c8f085050d7a4f0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Fri, 5 Oct 2018 00:11:09 +0300 Subject: [PATCH] Quote arguments in clang-target-wrapper.c We quote each passed through argument, and escape quote chars in them. For cases when the passed through argument ends with a backslash, this fails though, since whatever is unescaping it doesn't seem to handle double backslashes for escaping backslashes themselves. --- wrappers/clang-target-wrapper.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/wrappers/clang-target-wrapper.c b/wrappers/clang-target-wrapper.c index be3d776..b20f910 100644 --- a/wrappers/clang-target-wrapper.c +++ b/wrappers/clang-target-wrapper.c @@ -46,6 +46,25 @@ typedef char TCHAR; #define EXECVP_CAST (char **) #endif +TCHAR *escape(const TCHAR *str) { +#ifdef _WIN32 + TCHAR *out = malloc((_tcslen(str) * 2 + 3) * sizeof(*out)); + TCHAR *ptr = out; + int i; + *ptr++ = '"'; + for (i = 0; str[i]; i++) { + if (str[i] == '"') + *ptr++ = '\\'; + *ptr++ = str[i]; + } + *ptr++ = '"'; + *ptr++ = '\0'; + return out; +#else + return _tcsdup(str); +#endif +} + int _tmain(int argc, TCHAR* argv[]) { const TCHAR *argv0 = argv[0]; const TCHAR *sep = _tcsrchr(argv0, '/'); @@ -124,7 +143,7 @@ int _tmain(int argc, TCHAR* argv[]) { exec_argv[arg++] = _T("-Qunused-arguments"); for (int i = 1; i < argc; i++) - exec_argv[arg++] = argv[i]; + exec_argv[arg++] = escape(argv[i]); exec_argv[arg] = NULL; if (arg > max_arg) {