Skip to content

Commit b6967c3

Browse files
committed
attr_file: refactor stripping of trailing spaces
The stripping of trailing spaces currently happens as part of `git_attr_fnmatch__parse`. As we aren't currently parsing trailing whitespaces correct in case they're escaped, we'll have to change that code, though. To make actual behavioural change easier to review, refactor the code up-front by pulling it out into its own function that is expected to retain the exact same functionality as before. Like this, the fix will be trivial to apply.
1 parent b1795e0 commit b6967c3

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/attr_file.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,19 @@ void git_attr_path__free(git_attr_path *info)
559559
* "cat-file.c" but not "mozilla-sha1/sha1.c".
560560
*/
561561

562+
/*
563+
* Determine the length of trailing spaces.
564+
*/
565+
static size_t trailing_space_length(const char *p, size_t len)
566+
{
567+
size_t n;
568+
for (n = len; n; n--) {
569+
if (p[n-1] != ' ' && p[n-1] != '\t')
570+
break;
571+
}
572+
return len - n;
573+
}
574+
562575
/*
563576
* This will return 0 if the spec was filled out,
564577
* GIT_ENOTFOUND if the fnmatch does not require matching, or
@@ -646,9 +659,10 @@ int git_attr_fnmatch__parse(
646659
return GIT_ENOTFOUND;
647660

648661
/* Remove trailing spaces. */
649-
while (pattern[spec->length - 1] == ' ' || pattern[spec->length - 1] == '\t')
650-
if (--spec->length == 0)
651-
return GIT_ENOTFOUND;
662+
spec->length -= trailing_space_length(pattern, spec->length);
663+
664+
if (spec->length == 0)
665+
return GIT_ENOTFOUND;
652666

653667
if (pattern[spec->length - 1] == '/') {
654668
spec->length--;

0 commit comments

Comments
 (0)