From 3a078dec3327fb68faa7c800040f89db3e38e6e5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Tue, 1 Jan 2013 09:44:02 +0700 Subject: [PATCH] wildmatch: fix "**" special case MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit "**" is adjusted to only be effective when surrounded by slashes, in 40bbee0 (wildmatch: adjust "**" behavior - 2012-10-15). Except that the commit did it wrong: 1. when it checks for "the preceding slash unless ** is at the beginning", it compares to wrong pointer. It should have compared to the beginning of the pattern, not the text. 2. prev_p points to the character before "**", not the first "*". The correct comparison must be "prev_p < pattern" or "prev_p + 1 == pattern", not "prev_p == pattern". 3. The pattern must be surrounded by slashes unless it's at the beginning or the end of the pattern. We do two checks: one for the preceding slash and one the trailing slash. Both checks must be met. The use of "||" is wrong. This patch fixes all above. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- t/t3070-wildmatch.sh | 2 +- wildmatch.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/t/t3070-wildmatch.sh b/t/t3070-wildmatch.sh index d5bafefbe..af54c8311 100755 --- a/t/t3070-wildmatch.sh +++ b/t/t3070-wildmatch.sh @@ -83,7 +83,7 @@ match 0 0 'deep/foo/bar/baz/' '**/bar/*' match 1 0 'deep/foo/bar/baz/' '**/bar/**' match 0 0 'deep/foo/bar' '**/bar/*' match 1 0 'deep/foo/bar/' '**/bar/**' -match 1 0 'foo/bar/baz' '**/bar**' +match 0 0 'foo/bar/baz' '**/bar**' match 1 0 'foo/bar/baz/x' '*/bar/**' match 0 0 'deep/foo/bar/baz/x' '*/bar/**' match 1 0 'deep/foo/bar/baz/x' '**/bar/*/*' diff --git a/wildmatch.c b/wildmatch.c index 3972e26e8..5f976e91d 100644 --- a/wildmatch.c +++ b/wildmatch.c @@ -58,6 +58,7 @@ typedef unsigned char uchar; static int dowild(const uchar *p, const uchar *text, int force_lower_case) { uchar p_ch; + const uchar *pattern = p; for ( ; (p_ch = *p) != '\0'; text++, p++) { int matched, special; @@ -87,7 +88,7 @@ static int dowild(const uchar *p, const uchar *text, int force_lower_case) if (*++p == '*') { const uchar *prev_p = p - 2; while (*++p == '*') {} - if ((prev_p == text || *prev_p == '/') || + if ((prev_p < pattern || *prev_p == '/') && (*p == '\0' || *p == '/' || (p[0] == '\\' && p[1] == '/'))) { /* -- 2.26.2