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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
diff --git a/src/attr_file.c b/src/attr_file.c
index aef3e64..b2c60f2 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -560,6 +560,19 @@ void git_attr_path__free(git_attr_path *info)
*/
/*
+ * Determine the length of trailing spaces.
+ */
+static size_t trailing_space_length(const char *p, size_t len)
+{
+ size_t n;
+ for (n = len; n; n--) {
+ if (p[n-1] != ' ' && p[n-1] != '\t')
+ break;
+ }
+ return len - n;
+}
+
+/*
* This will return 0 if the spec was filled out,
* GIT_ENOTFOUND if the fnmatch does not require matching, or
* another error code there was an actual problem.
@@ -646,9 +659,10 @@ int git_attr_fnmatch__parse(
return GIT_ENOTFOUND;
/* Remove trailing spaces. */
- while (pattern[spec->length - 1] == ' ' || pattern[spec->length - 1] == '\t')
- if (--spec->length == 0)
- return GIT_ENOTFOUND;
+ spec->length -= trailing_space_length(pattern, spec->length);
+
+ if (spec->length == 0)
+ return GIT_ENOTFOUND;
if (pattern[spec->length - 1] == '/') {
spec->length--;