git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/2] attr: more matching optimizations from .gitignore

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 10, 2012, 21:41 UTC
Message-ID
<7v626iatt9.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1349864466-28289-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 9 quoted lines
>    @@ -690,16 +689,18 @@ static int path_matches(const char *pathname, int pathlen,
>     	 * contain the trailing slash
>     	 */
>     
>    -	if (pathlen < baselen ||
>    +	if (pathlen < baselen + 1 ||
>     	    (baselen && pathname[baselen] != '/') ||
>    -	    strncmp(pathname, base, baselen))
>    +	    strncmp_icase(pathname, base, baselen))
Shouldn't the last comparison be
	strncmp_icase(pathname, base, baselen + 1)

instead, if you are trying to match this part from dir.c where baselen does count the trailing slash?

		if (pathlen < x->baselen ||
		    (x->baselen && pathname[x->baselen-1] != '/') ||
		    strncmp_icase(pathname, x->base, x->baselen))
			continue;

In other words, relative to what was queued to 'pu', something like this instead....

-- >8 --
Subject: [PATCH] fixup: matching path_matches() in attr.c to that of dir.c

In this function, baselen does not count the trailing slash that should come after the directory name held in "basename" variable, so whenever the corresponding code in dir.c:excluded_from_list() refers to x->baselen, we would need to use "baselen+1" consistently.

Also remove unnecessary NUL-termination of a buffer obtained from calloc().

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 attr.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/attr.c b/attr.c
index 7e85f82..a9c04a8 100644
--- a/attr.c
+++ b/attr.c
@@ -250,7 +250,6 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 	else {
 		char *p = (char *)&(res->state[num_attr]);
 		memcpy(p, name, namelen);
-		p[namelen] = 0;
 		res->u.pat.pattern = p;
 		parse_exclude_pattern(&res->u.pat.pattern,
 				      &res->u.pat.patternlen,
@@ -687,19 +686,21 @@ static int path_matches(const char *pathname, int pathlen,
 
 	/*
 	 * note: unlike excluded_from_list, baselen here does not
-	 * contain the trailing slash
+	 * count the trailing slash.
 	 */
 
-	if (pathlen < baselen ||
+	if (pathlen < baselen + 1 ||
 	    (baselen && pathname[baselen] != '/') ||
-	    strncmp(pathname, base, baselen))
+	    strncmp_icase(pathname, base, baselen + 1))
 		return 0;
 
 	namelen = baselen ? pathlen - baselen - 1 : pathlen;
 	name = pathname + pathlen - namelen;
 
-	/* if the non-wildcard part is longer than the remaining
-	   pathname, surely it cannot match */
+	/*
+	 * if the non-wildcard part is longer than the remaining
+	 * pathname, surely it cannot match.
+	 */
 	if (!namelen || prefix > namelen)
 		return 0;
 
-- 
1.8.0.rc1.76.g5a375e6
Previous: Nguyen Thai Ngoc DuyNext: Junio C Hamano
Message 10 of 14 in “gitignore: make pattern parsing code a separate function”
  1. 1/2 gitignore: make pattern parsing code a separate functionNguyễn Thái Ngọc Duy, Oct 9, 2012
  2. 2/2 attr: more matching optimizations from .gitignoreNguyễn Thái Ngọc Duy, Oct 9, 2012
  3. Junio C HamanoOct 9, 2012
  4. Johannes SixtOct 9, 2012
  5. Junio C HamanoOct 9, 2012
  6. Junio C HamanoOct 9, 2012
  7. 2/2 attr: more matching optimizations from .gitignoreNguyễn Thái Ngọc Duy, Oct 10, 2012
  8. Junio C HamanoOct 10, 2012
  9. Nguyen Thai Ngoc DuyOct 12, 2012
  10. Junio C HamanoOct 10, 2012
  11. Junio C HamanoOct 10, 2012
  12. Nguyen Thai Ngoc DuyOct 11, 2012
  13. Junio C HamanoOct 12, 2012
  14. Nguyen Thai Ngoc DuyOct 13, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.