Skip to content

Commit ab3aebc

Browse files
peffgitster
authored andcommitted
dir.c::match_pathname(): pay attention to the length of string parameters
This function takes two counted strings: a <pattern, patternlen> pair and a <pathname, pathlen> pair. But we end up feeding the result to fnmatch, which expects NUL-terminated strings. We can fix this by calling the fnmatch_icase_mem function, which handles re-allocating into a NUL-terminated string if necessary. While we're at it, we can avoid even calling fnmatch in some cases. In addition to patternlen, we get "prefix", the size of the pattern that contains no wildcard characters. We do a straight match of the prefix part first, and then use fnmatch to cover the rest. But if there are no wildcards in the pattern at all, we do not even need to call fnmatch; we would simply be comparing two empty strings. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 982ac87 commit ab3aebc

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

dir.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,22 @@ int match_pathname(const char *pathname, int pathlen,
624624
if (strncmp_icase(pattern, name, prefix))
625625
return 0;
626626
pattern += prefix;
627+
patternlen -= prefix;
627628
name += prefix;
628629
namelen -= prefix;
630+
631+
/*
632+
* If the whole pattern did not have a wildcard,
633+
* then our prefix match is all we need; we
634+
* do not need to call fnmatch at all.
635+
*/
636+
if (!patternlen && !namelen)
637+
return 1;
629638
}
630639

631-
return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
640+
return fnmatch_icase_mem(pattern, patternlen,
641+
name, namelen,
642+
FNM_PATHNAME) == 0;
632643
}
633644

634645
/* Scan the list and let the last match determine the fate.

0 commit comments

Comments
 (0)