summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2019-06-06 14:02:17 +0200
committerPatrick Steinhardt <ps@pks.im>2019-06-06 14:15:36 +0200
commitb6967c393aaa9bc8fcb1f248f94a4deb897248cb (patch)
tree98f2bdcb3229e49e3efb1c536703e2297b440342
parentb1795e0da2c5c1f9429fedd2fa0b679ccca0f586 (diff)
downloadlibgit2-b6967c393aaa9bc8fcb1f248f94a4deb897248cb.tar.gz
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.
-rw-r--r--src/attr_file.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/attr_file.c b/src/attr_file.c
index aef3e64af..b2c60f204 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--;