Git fork

wildmatch: avoid undefined behavior

The code changed in this commit is designed to check if the pattern
starts with "**/" or contains "/**/" (see 3a078dec33 (wildmatch: fix
"**" special case, 2013-01-01)). Unfortunately when the pattern begins
with "**/" `prev_p = p - 2` is evaluated when `p` points to the second
"*" and so the subtraction is undefined according to section 6.5.6 of
the C standard because the result does not point within the same object
as `p`. Fix this by avoiding the subtraction unless it is well defined.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

authored by

Phillip Wood and committed by
Junio C Hamano
81b26f8f 1f2e05f0

+2 -2
+2 -2
wildmatch.c
··· 83 continue; 84 case '*': 85 if (*++p == '*') { 86 - const uchar *prev_p = p - 2; 87 while (*++p == '*') {} 88 if (!(flags & WM_PATHNAME)) 89 /* without WM_PATHNAME, '*' == '**' */ 90 match_slash = 1; 91 - else if ((prev_p < pattern || *prev_p == '/') && 92 (*p == '\0' || *p == '/' || 93 (p[0] == '\\' && p[1] == '/'))) { 94 /*
··· 83 continue; 84 case '*': 85 if (*++p == '*') { 86 + const uchar *prev_p = p; 87 while (*++p == '*') {} 88 if (!(flags & WM_PATHNAME)) 89 /* without WM_PATHNAME, '*' == '**' */ 90 match_slash = 1; 91 + else if ((prev_p - pattern < 2 || *(prev_p - 2) == '/') && 92 (*p == '\0' || *p == '/' || 93 (p[0] == '\\' && p[1] == '/'))) { 94 /*