Skip to content

Commit a5e916c

Browse files
newrengitster
authored andcommitted
dir: fix off-by-one error in match_pathspec_item
For a pathspec like 'foo/bar' comparing against a path named "foo/", namelen will be 4, and match[namelen] will be 'b'. The correct location of the directory separator is namelen-1. However, other callers of match_pathspec_item() such as builtin/grep.c's submodule_path_match() will compare against a path named "foo" instead of "foo/". It might be better to change all the callers to be consistent, as discussed at https://public-inbox.org/git/xmqq7e6cdnkr.fsf@gitster-ct.c.googlers.com/ and https://public-inbox.org/git/CABPp-BERWUPCPq-9fVW1LNocqkrfsoF4BPj3gJd9+En43vEkTQ@mail.gmail.com/ but there are many cases to audit, so for now just make sure we handle both cases with and without a trailing slash. The reason the code worked despite this sometimes-off-by-one error was that the subsequent code immediately checked whether the first matchlen characters matched (which they do) and then bailed and return MATCHED_RECURSIVELY anyway since wildmatch doesn't have the ability to check if "name" can be matched as a directory (or prefix) against the pathspec. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bbbb6b0 commit a5e916c

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

dir.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ static int match_pathspec_item(const struct index_state *istate,
356356
/* Perform checks to see if "name" is a super set of the pathspec */
357357
if (flags & DO_MATCH_SUBMODULE) {
358358
/* name is a literal prefix of the pathspec */
359+
int offset = name[namelen-1] == '/' ? 1 : 0;
359360
if ((namelen < matchlen) &&
360-
(match[namelen] == '/') &&
361+
(match[namelen-offset] == '/') &&
361362
!ps_strncmp(item, match, name, namelen))
362363
return MATCHED_RECURSIVELY;
363364

0 commit comments

Comments
 (0)