Skip to content

Commit 20c71bc

Browse files
committed
Merge branch 'fix-msys2-quoting-bugs'
These patches fix several bugs in quoting arguments when spawning shell scripts on Windows. Note: these bugs are Windows-only, as we have to construct a command line for the process-to-spawn, unlike Linux/macOS, where `execv()` accepts an already-split command line. Furthermore, these fixes were not included in the CVE-2019-1350 part of v2.14.6 because the Windows-specific quoting when spawning shell scripts was contributed from Git for Windows into Git only in the v2.21.x era. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents d9061ed + 7d8b676 commit 20c71bc

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

compat/mingw.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,13 +1120,14 @@ static const char *quote_arg_msys2(const char *arg)
11201120

11211121
for (p = arg; *p; p++) {
11221122
int ws = isspace(*p);
1123-
if (!ws && *p != '\\' && *p != '"' && *p != '{')
1123+
if (!ws && *p != '\\' && *p != '"' && *p != '{' && *p != '\'' &&
1124+
*p != '?' && *p != '*' && *p != '~')
11241125
continue;
11251126
if (!buf.len)
11261127
strbuf_addch(&buf, '"');
11271128
if (p != p2)
11281129
strbuf_add(&buf, p2, p - p2);
1129-
if (!ws && *p != '{')
1130+
if (*p == '\\' || *p == '"')
11301131
strbuf_addch(&buf, '\\');
11311132
p2 = p;
11321133
}
@@ -1136,7 +1137,7 @@ static const char *quote_arg_msys2(const char *arg)
11361137
else if (!buf.len)
11371138
return arg;
11381139
else
1139-
strbuf_add(&buf, p2, p - p2),
1140+
strbuf_add(&buf, p2, p - p2);
11401141

11411142
strbuf_addch(&buf, '"');
11421143
return strbuf_detach(&buf, 0);
@@ -1391,7 +1392,10 @@ static inline int match_last_path_component(const char *path, size_t *len,
13911392

13921393
static int is_msys2_sh(const char *cmd)
13931394
{
1394-
if (cmd && !strcmp(cmd, "sh")) {
1395+
if (!cmd)
1396+
return 0;
1397+
1398+
if (!strcmp(cmd, "sh")) {
13951399
static int ret = -1;
13961400
char *p;
13971401

@@ -1411,6 +1415,16 @@ static int is_msys2_sh(const char *cmd)
14111415
}
14121416
return ret;
14131417
}
1418+
1419+
if (ends_with(cmd, "\\sh.exe")) {
1420+
static char *sh;
1421+
1422+
if (!sh)
1423+
sh = path_lookup("sh", 0);
1424+
1425+
return !fspathcmp(cmd, sh);
1426+
}
1427+
14141428
return 0;
14151429
}
14161430

@@ -1426,7 +1440,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14261440
BOOL ret;
14271441
HANDLE cons;
14281442
const char *(*quote_arg)(const char *arg) =
1429-
is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc;
1443+
is_msys2_sh(cmd ? cmd : *argv) ?
1444+
quote_arg_msys2 : quote_arg_msvc;
14301445

14311446
do_unset_environment_variables();
14321447

0 commit comments

Comments
 (0)