Skip to content

Commit 3036485

Browse files
committed
fixup! mingw: special-case arguments to sh
While working on parallelizing the tests in Azure Pipelines, an issue was discovered with the `is_msys2_sh()` function: it expects the path components to be separated by exactly one dir separator. That does not need to be the case, though, e.g. when the components in the `PATH` variable have trailing slashes. Let's make the code much more robust in this respect. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent a7f8b0d commit 3036485

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

compat/mingw.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,21 @@ struct pinfo_t {
16281628
static struct pinfo_t *pinfo = NULL;
16291629
CRITICAL_SECTION pinfo_cs;
16301630

1631+
/* Used to match and chomp off path components */
1632+
static inline int match_last_path_component(const char *path, size_t *len,
1633+
const char *component)
1634+
{
1635+
size_t component_len = strlen(component);
1636+
if (*len < component_len + 1 ||
1637+
!is_dir_sep(path[*len - component_len - 1]) ||
1638+
strncasecmp(path + *len - component_len, component, component_len))
1639+
return 0;
1640+
*len -= component_len + 1;
1641+
while (*len > 0 && is_dir_sep(path[*len - 1]))
1642+
(*len)--;
1643+
return 1;
1644+
}
1645+
16311646
static int is_msys2_sh(const char *cmd)
16321647
{
16331648
if (cmd && !strcmp(cmd, "sh")) {
@@ -1642,13 +1657,10 @@ static int is_msys2_sh(const char *cmd)
16421657
ret = 0;
16431658
else {
16441659
size_t len = strlen(p);
1645-
ret = len > 15 &&
1646-
is_dir_sep(p[len - 15]) &&
1647-
!strncasecmp(p + len - 14, "usr", 3) &&
1648-
is_dir_sep(p[len - 11]) &&
1649-
!strncasecmp(p + len - 10, "bin", 3) &&
1650-
is_dir_sep(p[len - 7]) &&
1651-
!strcasecmp(p + len - 6, "sh.exe");
1660+
1661+
ret = match_last_path_component(p, &len, "sh.exe") &&
1662+
match_last_path_component(p, &len, "bin") &&
1663+
match_last_path_component(p, &len, "usr");
16521664
free(p);
16531665
}
16541666
return ret;

0 commit comments

Comments
 (0)