git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / Atom feed
From: Elijah Newren <newren@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Git Mailing List <git@vger.kernel.org>, sxlijin@gmail.com
Subject: Re: [RFC PATCH 2/7] dir.c: fix off-by-one error in match_pathspec_item
Date: Thu, 5 Apr 2018 11:36:45 -0700
Message-ID: <CABPp-BERWUPCPq-9fVW1LNocqkrfsoF4BPj3gJd9+En43vEkTQ@mail.gmail.com> (raw)
In-Reply-To: <20180405174925.GA19974@sigill.intra.peff.net>

On Thu, Apr 5, 2018 at 10:49 AM, Jeff King <peff@peff.net> wrote:
>> diff --git a/dir.c b/dir.c
>> index 19212129f0..c915a69385 100644
>> --- a/dir.c
>> +++ b/dir.c
>> @@ -384,7 +384,7 @@ static int match_pathspec_item(const struct pathspec_item *item, int prefix,
>>       if (flags & DO_MATCH_SUBMODULE) {
>>               /* name is a literal prefix of the pathspec */
>>               if ((namelen < matchlen) &&
>> -                 (match[namelen] == '/') &&
>> +                 (match[namelen-1] == '/') &&
>>                   !ps_strncmp(item, match, name, namelen))
>>                       return MATCHED_RECURSIVELY;
>
> Do we care about matching the name "foo" against the patchspec_item "foo/"?
>
> That matches now, but wouldn't after your patch.

Technically, the tests pass anyway due to the fallback behavior
mentioned in the commit message, but this is a really good point.  It
looks like the call to submodule_path_match() from builtin/grep.c is
going to be passing name without the trailing '/', which is contrary
to how read_directory_recursive() in dir.c builds up paths (namely
with the trailing '/'). If we tried to force consistency (either
always omit the trailing slash or always include it), then we'd
probably want to do so for match_pathspec() calls as well, and there
are lots of those throughout the code and auditing it all looks
painful.

So I should probably make the check handle both cases:

@@ -383,8 +383,9 @@ static int match_pathspec_item(const struct
pathspec_item *item, int prefix,
        /* Perform checks to see if "name" is a super set of the pathspec */
        if (flags & DO_MATCH_LEADING_PATHSPEC) {
                /* name is a literal prefix of the pathspec */
+               int offset = name[namelen-1] == '/' ? 1 : 0;
                if ((namelen < matchlen) &&
-                   (match[namelen] == '/') &&
+                   (match[namelen-offset] == '/') &&
                    !ps_strncmp(item, match, name, namelen))
                        return MATCHED_RECURSIVELY_LEADING_PATHSPEC;

  reply index

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-05 17:34 [RFC PATCH 0/7] Fix `git clean` with pathspecs Elijah Newren
2018-04-05 17:34 ` [RFC PATCH 1/7] dir.c: Fix typo in comment Elijah Newren
2018-04-05 17:34 ` [RFC PATCH 2/7] dir.c: fix off-by-one error in match_pathspec_item Elijah Newren
2018-04-05 17:49   ` Jeff King
2018-04-05 18:36     ` Elijah Newren [this message]
2018-04-05 19:04       ` Jeff King
2018-04-05 20:06         ` Elijah Newren
2018-04-06 17:53           ` Jeff King
2018-04-05 17:34 ` [RFC PATCH 3/7] t7300: Add some testcases showing failure to clean specified pathspecs Elijah Newren
2018-04-05 17:34 ` [RFC PATCH 4/7] dir: Directories should be checked for matching pathspecs too Elijah Newren
2018-04-05 18:58   ` Jeff King
2018-04-05 19:15     ` Elijah Newren
2018-04-05 19:31       ` Jeff King
2018-04-09  2:07         ` Junio C Hamano
2018-04-05 17:34 ` [RFC PATCH 5/7] dir: Make the DO_MATCH_SUBMODULE code reusable for a non-submodule case Elijah Newren
2018-04-05 17:34 ` [RFC PATCH 6/7] dir: If our pathspec might match files under a dir, recurse into it Elijah Newren
2018-04-05 17:34 ` [RFC PATCH 7/7] If we do not want globs to recurse into subdirs without -d Elijah Newren

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=CABPp-BERWUPCPq-9fVW1LNocqkrfsoF4BPj3gJd9+En43vEkTQ@mail.gmail.com \
    --to=newren@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=sxlijin@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

git@vger.kernel.org list mirror (unofficial, one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git