All of lore.kernel.org
 help / color / mirror / Atom feed
From: Duy Nguyen <pclouds@gmail.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, "Jean-Noël AVILA" <avila.jn@gmail.com>
Subject: Re: [regression?] trailing slash required in .gitattributes
Date: Sat, 23 Mar 2013 11:18:24 +0700	[thread overview]
Message-ID: <20130323041824.GA11142@lanh> (raw)
In-Reply-To: <20130322222438.GA13207@sigill.intra.peff.net>

On Fri, Mar 22, 2013 at 06:24:39PM -0400, Jeff King wrote:
> I'm having trouble figuring out the right solution for this.

Thanks for looking into this. It was on my todo list, but you beat me
to it :)

> But then here we'll end up feeding "foo/" to be compared with "foo",
> which we don't want. For a pattern "foo", we want to match _either_
> "foo/" or "foo". So you'd think something like:
> 
>   if (pathlen && pathname[pathlen-1] == '/')
>           pathlen--;
> 
> would work. But it seems that match_basename, despite taking the length
> of all of the strings we pass it, will happily use NUL-terminated
> functions like strcmp or fnmatch. Converting the former to check lengths
> should be pretty straightforward. But there is no version of fnmatch
> that does what we want. I wonder if we using wildmatch can get around
> this limitation.

You can use nwildmatch() from this patch. I tested it lightly with
t3070-wildmatch.sh, feeding the strings with no terminating NUL. It
seems to work ok.

-- 8< --
Subject: [PATCH] wildmatch: do not require "text" to be NUL-terminated

This may be helpful when we just want to match a part of "text".
nwildmatch can be used for this purpose.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 wildmatch.c | 25 +++++++++++++------------
 wildmatch.h | 11 +++++++++--
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/wildmatch.c b/wildmatch.c
index 7192bdc..f97ae2a 100644
--- a/wildmatch.c
+++ b/wildmatch.c
@@ -52,7 +52,8 @@ typedef unsigned char uchar;
 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
 
 /* Match pattern "p" against "text" */
-static int dowild(const uchar *p, const uchar *text, unsigned int flags)
+static int dowild(const uchar *p, const uchar *text,
+		  const uchar *textend, unsigned int flags)
 {
 	uchar p_ch;
 	const uchar *pattern = p;
@@ -60,8 +61,9 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 	for ( ; (p_ch = *p) != '\0'; text++, p++) {
 		int matched, match_slash, negated;
 		uchar t_ch, prev_ch;
-		if ((t_ch = *text) == '\0' && p_ch != '*')
+		if (text >= textend && p_ch != '*')
 			return WM_ABORT_ALL;
+		t_ch = *text;
 		if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 			t_ch = tolower(t_ch);
 		if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
@@ -101,7 +103,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					 * both foo/bar and foo/a/bar.
 					 */
 					if (p[0] == '/' &&
-					    dowild(p + 1, text, flags) == WM_MATCH)
+					    dowild(p + 1, text, textend, flags) == WM_MATCH)
 						return WM_MATCH;
 					match_slash = 1;
 				} else
@@ -130,9 +132,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 				/* the slash is consumed by the top-level for loop */
 				break;
 			}
-			while (1) {
-				if (t_ch == '\0')
-					break;
+			while (text < textend) {
 				/*
 				 * Try to advance faster when an asterisk is
 				 * followed by a literal. We know in this case
@@ -145,18 +145,18 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					p_ch = *p;
 					if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
 						p_ch = tolower(p_ch);
-					while ((t_ch = *text) != '\0' &&
+					while (text < textend &&
 					       (match_slash || t_ch != '/')) {
 						if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 							t_ch = tolower(t_ch);
 						if (t_ch == p_ch)
 							break;
-						text++;
+						t_ch = *++text;
 					}
 					if (t_ch != p_ch)
 						return WM_NOMATCH;
 				}
-				if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
+				if ((matched = dowild(p, text, textend, flags)) != WM_NOMATCH) {
 					if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
 						return matched;
 				} else if (!match_slash && t_ch == '/')
@@ -261,12 +261,13 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 		}
 	}
 
-	return *text ? WM_NOMATCH : WM_MATCH;
+	return text < textend ? WM_NOMATCH : WM_MATCH;
 }
 
 /* Match the "pattern" against the "text" string. */
-int wildmatch(const char *pattern, const char *text,
+int nwildmatch(const char *pattern, const char *text, int textlen,
 	      unsigned int flags, struct wildopts *wo)
 {
-	return dowild((const uchar*)pattern, (const uchar*)text, flags);
+	return dowild((const uchar*)pattern, (const uchar*)text,
+		      (const uchar*)text + textlen, flags);
 }
diff --git a/wildmatch.h b/wildmatch.h
index 4090c8f..cdd7544 100644
--- a/wildmatch.h
+++ b/wildmatch.h
@@ -12,7 +12,14 @@
 
 struct wildopts;
 
-int wildmatch(const char *pattern, const char *text,
-	      unsigned int flags,
+int nwildmatch(const char *pattern, const char *text,
+	      int len, unsigned int flags,
 	      struct wildopts *wo);
+
+/* Match the "pattern" against the "text" string. */
+static inline int wildmatch(const char *pattern, const char *text,
+	      unsigned int flags, struct wildopts *wo)
+{
+	return nwildmatch(pattern, text, strlen(text), flags, wo);
+}
 #endif
-- 
1.8.2.83.gc99314b

-- 8< --

  parent reply	other threads:[~2013-03-23  4:18 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-19 17:57 [regression?] trailing slash required in .gitattributes Jeff King
2013-03-19 18:10 ` Junio C Hamano
2013-03-19 18:10 ` Jeff King
2013-03-22 22:24   ` Jeff King
2013-03-22 23:08     ` Junio C Hamano
2013-03-23  8:39       ` Jeff King
2013-03-24  5:25         ` Junio C Hamano
2013-03-26 18:39         ` [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward Junio C Hamano
2013-03-26 18:39           ` [PATCH 1/4] attr.c::path_matches(): the basename is part of the pathname Junio C Hamano
2013-03-26 18:49             ` Jeff King
2013-03-27  1:40               ` Duy Nguyen
2013-03-26 18:39           ` [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters Junio C Hamano
2013-03-26 18:55             ` Jeff King
2013-03-26 20:39               ` Jeff King
2013-03-26 20:49                 ` Junio C Hamano
2013-03-26 21:29                   ` Jeff King
2013-03-26 22:33                     ` Junio C Hamano
2013-03-27  1:04                       ` Jeff King
2013-03-26 18:39           ` [PATCH 3/4] attr.c::path_matches(): special case paths that end with a slash Junio C Hamano
2013-03-26 19:05             ` Jeff King
2013-03-26 21:33               ` Jeff King
2013-03-27  1:30                 ` Duy Nguyen
2013-03-28 19:49               ` Jeff King
2013-03-26 18:39           ` [PATCH 4/4] make sure a pattern without trailing slash matches a directory Junio C Hamano
2013-03-26 19:08             ` Jeff King
2013-03-27  1:13           ` [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward Duy Nguyen
2013-03-27  3:57             ` Junio C Hamano
2013-03-27  4:01               ` Duy Nguyen
2013-03-28 21:43           ` [PATCH v2 0/6] " Jeff King
2013-03-28 21:45             ` [PATCH 1/6] attr.c::path_matches(): the basename is part of the pathname Jeff King
2013-03-28 21:47             ` [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters Jeff King
2013-03-28 22:40               ` Jeff King
2013-03-28 22:49                 ` Jeff King
2013-03-28 23:10                   ` Junio C Hamano
2013-03-28 23:40                   ` Duy Nguyen
2013-03-29  1:25               ` Duy Nguyen
2013-03-29  3:02                 ` Jeff King
2013-03-29  5:57                   ` Junio C Hamano
2013-03-28 21:47             ` [PATCH 3/6] dir.c::match_pathname(): adjust patternlen when shifting pattern Jeff King
2013-03-28 21:48             ` [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters Jeff King
2013-03-28 22:30               ` Junio C Hamano
2013-03-29  8:45               ` Duy Nguyen
2013-03-29 10:03                 ` Duy Nguyen
2013-03-29 11:32                   ` Torsten Bögershausen
2013-03-29 11:37                     ` Duy Nguyen
2013-03-29 12:05                 ` Jeff King
2013-03-29 13:02                   ` Duy Nguyen
2013-03-29 16:44                     ` Junio C Hamano
2013-03-29 17:04                       ` Jeff King
2013-03-29 17:35                         ` Junio C Hamano
2013-03-29 17:44                           ` Jeff King
2013-03-30  1:40                       ` Duy Nguyen
2013-03-28 21:49             ` [PATCH 5/6] attr.c::path_matches(): special case paths that end with a slash Jeff King
2013-03-28 21:50             ` [PATCH 6/6] t: check that a pattern without trailing slash matches a directory Jeff King
2013-03-28 22:21               ` Eric Sunshine
2013-03-28 22:22                 ` Jeff King
2013-03-23  4:18     ` Duy Nguyen [this message]
2013-03-23  4:43       ` [regression?] trailing slash required in .gitattributes Duy Nguyen
2013-03-25  6:05 ` [PATCH 0/4] attr directory matching regression Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 1/4] wildmatch: do not require "text" to be NUL-terminated Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 2/4] attr.c: fix pattern{,len} inconsistency in struct match_attr Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 3/4] dir.c: make match_{base,path}name respect {basename,path}len Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 4/4] attr.c: fix matching "subdir" without the trailing slash Nguyễn Thái Ngọc Duy
2013-03-25  7:20     ` Duy Nguyen
2013-03-25  9:24       ` Duy Nguyen
2013-03-26 15:10   ` [PATCH 0/4] attr directory matching regression Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130323041824.GA11142@lanh \
    --to=pclouds@gmail.com \
    --cc=avila.jn@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.