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

Re: [RFC PATCH 2/7] dir.c: fix off-by-one error in match_pathspec_item

From
Jeff King <peff@peff.net>
Date
Apr 5, 2018, 19:04 UTC
Message-ID
<20180405190446.GB21164@sigill.intra.peff.net>
In-Reply-To
<CABPp-BERWUPCPq-9fVW1LNocqkrfsoF4BPj3gJd9+En43vEkTQ@mail.gmail.com>
On Thu, Apr 05, 2018 at 11:36:45AM -0700, Elijah Newren wrote:
Show 28 quoted lines
> > 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;

That seems reasonable to me, and your "offset" trick here should prevent us from getting confused. Can namelen ever be zero here? I guess probably not (I could see an empty pathspec, but an empty path does not make sense).

There are other similar trailing-slash matches in that function, but I'm not sure of all the cases in which they're used. I don't know if any of those would need similar treatment (sorry for being vague; I expect I'd need a few hours to dig into how the pathspec code actually works, and I don't have that today).

-Peff
Previous: Elijah NewrenNext: Elijah Newren
Message 15 of 17 in “Fix `git clean` with pathspecs”
  1. 0/7 Fix `git clean` with pathspecsElijah Newren, Apr 5, 2018
  2. 7/7 If we do not want globs to recurse into subdirs without -d...Elijah Newren, Apr 5, 2018
  3. 6/7 dir: If our pathspec might match files under a dir, recurse into itElijah Newren, Apr 5, 2018
  4. 4/7 dir: Directories should be checked for matching pathspecs tooElijah Newren, Apr 5, 2018
  5. Jeff KingApr 5, 2018
  6. Elijah NewrenApr 5, 2018
  7. Jeff KingApr 5, 2018
  8. Junio C HamanoApr 9, 2018
  9. 3/7 t7300: Add some testcases showing failure to clean specified pathspecsElijah Newren, Apr 5, 2018
  10. 5/7 dir: Make the DO_MATCH_SUBMODULE code reusable for a non-submodule caseElijah Newren, Apr 5, 2018
  11. 1/7 dir.c: Fix typo in commentElijah Newren, Apr 5, 2018
  12. 2/7 dir.c: fix off-by-one error in match_pathspec_itemElijah Newren, Apr 5, 2018
  13. Jeff KingApr 5, 2018
  14. Elijah NewrenApr 5, 2018
  15. Jeff KingApr 5, 2018
  16. Elijah NewrenApr 5, 2018
  17. Jeff KingApr 6, 2018

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.