Commit b6967c393aaa9bc8fcb1f248f94a4deb897248cb

Patrick Steinhardt 2019-06-06T14:02:17

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.

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--;