git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, pclouds@gmail.com, avila.jn@gmail.com
Subject: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters
Date: Thu, 28 Mar 2013 17:47:28 -0400	[thread overview]
Message-ID: <20130328214728.GB10936@sigill.intra.peff.net> (raw)
In-Reply-To: <20130328214358.GA10685@sigill.intra.peff.net>

From: Junio C Hamano <gitster@pobox.com>

The function takes two counted strings (<basename, basenamelen> and
<pattern, patternlen>) as parameters, together with prefix (the
length of the prefix in pattern that is to be matched literally
without globbing against the basename) and EXC_* flags that tells it
how to match the pattern against the basename.

However, it did not pay attention to the length of these counted
strings.  Update them to do the following:

 * When the entire pattern is to be matched literally, the pattern
   matches the basename only when the lengths of them are the same,
   and they match up to that length.

 * When the pattern is "*" followed by a string to be matched
   literally, make sure that the basenamelen is equal or longer than
   the "literal" part of the pattern, and the tail of the basename
   string matches that literal part.

 * Otherwise, use the new fnmatch_icase_mem helper to make
   sure we only lookmake sure we use only look at the
   counted part of the strings.  Because these counted strings are
   full strings most of the time, we check for termination
   to avoid unnecessary allocation.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jeff King <peff@peff.net>
---
Compared to v1:

  - This factors the fnmatch bits into a helper function so we can reuse it
    later. As a result, the variable names are changed a bit.

  - The original did:

      if (use_pat)
              strbuf_release(&pat);

    but AFAICT that was a useless conditional; use_pat always points to
    either the incoming buffer or the strbuf. But strbuf_release will
    handle both cases for us.

 dir.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)

diff --git a/dir.c b/dir.c
index 5a83aa7..fac82c1 100644
--- a/dir.c
+++ b/dir.c
@@ -34,6 +34,33 @@ int fnmatch_icase(const char *pattern, const char *string, int flags)
 	return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
 }
 
+static int fnmatch_icase_mem(const char *pattern, int patternlen,
+			     const char *string, int stringlen,
+			     int flags)
+{
+	int match_status;
+	struct strbuf pat_buf = STRBUF_INIT;
+	struct strbuf str_buf = STRBUF_INIT;
+	const char *use_pat = pattern;
+	const char *use_str = string;
+
+	if (pattern[patternlen]) {
+		strbuf_add(&pat_buf, pattern, patternlen);
+		use_pat = pat_buf.buf;
+	}
+	if (string[stringlen]) {
+		strbuf_add(&str_buf, string, stringlen);
+		use_str = str_buf.buf;
+	}
+
+	match_status = fnmatch_icase(use_pat, use_str, 0);
+
+	strbuf_release(&pat_buf);
+	strbuf_release(&str_buf);
+
+	return match_status;
+}
+
 static size_t common_prefix_len(const char **pathspec)
 {
 	const char *n, *first;
@@ -537,15 +564,20 @@ int match_basename(const char *basename, int basenamelen,
 		   int flags)
 {
 	if (prefix == patternlen) {
-		if (!strcmp_icase(pattern, basename))
+		if (patternlen == basenamelen &&
+		    !strncmp_icase(pattern, basename, basenamelen))
 			return 1;
 	} else if (flags & EXC_FLAG_ENDSWITH) {
+		/* "*literal" matching against "fooliteral" */
 		if (patternlen - 1 <= basenamelen &&
-		    !strcmp_icase(pattern + 1,
-				  basename + basenamelen - patternlen + 1))
+		    !strncmp_icase(pattern + 1,
+				   basename + basenamelen - (patternlen - 1),
+				   patternlen - 1))
 			return 1;
 	} else {
-		if (fnmatch_icase(pattern, basename, 0) == 0)
+		if (fnmatch_icase_mem(pattern, patternlen,
+				      basename, basenamelen,
+				      0) == 0)
 			return 1;
 	}
 	return 0;
-- 
1.8.2.13.g0f18d3c

  parent reply	other threads:[~2013-03-28 21:48 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             ` Jeff King [this message]
2013-03-28 22:40               ` [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters 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     ` [regression?] trailing slash required in .gitattributes Duy Nguyen
2013-03-23  4:43       ` 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=20130328214728.GB10936@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=avila.jn@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).