word diff: handle zero length matches

If find_word_boundaries() encounters a zero length match (which can be caused by matching a newline or using '*' instead of '+' in the regex) we stop splitting the input into words which generates an inaccurate diff. To fix this increment the start point when there is a zero length match and try a new match. This is safe as posix regular expressions always return the longest available match so a zero length match means there are no longer matches available from the current position. Commit bf82940dbf1 (color-words: enable REG_NEWLINE to help user, 2009-01-17) prevented matching newlines in negated character classes but it is still possible for the user to have an explicit newline match in the regex which could cause a zero length match. One could argue that having explicit newline matches or using '*' rather than '+' are user errors but it seems to be better to work round them than produce inaccurate diffs. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed May 4, 2021 at 09:27 UTC 0324e8fc6b297c9e61745dc4e7d110780334157d
2 files changed +12 -3
diff.c
+7 -3
@@ -2053,7 +2053,7 @@ static void fn_out_diff_words_aux(void *priv,
2053 static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2054 int *begin, int *end)
2055 {
2056 - if (word_regex && *begin < buffer->size) {
2056 + while (word_regex && *begin < buffer->size) {
2057 regmatch_t match[1];
2058 if (!regexec_buf(word_regex, buffer->ptr + *begin,
2059 buffer->size - *begin, 1, match, 0)) {
@@ -2061,9 +2061,13 @@ static int find_word_boundaries(mmfile_t *buffer, regex_t *word_regex,
2061 '\n', match[0].rm_eo - match[0].rm_so);
2062 *end = p ? p - buffer->ptr : match[0].rm_eo + *begin;
2063 *begin += match[0].rm_so;
2064 - return *begin >= *end;
2064 + if (*begin == *end)
2065 + (*begin)++;
2066 + else
2067 + return *begin > *end;
2068 + } else {
2069 + return -1;
2070 }
2066 - return -1;
2071 }
2072
2073 /* find the next word */
t/t4034-diff-words.sh
+5
@@ -184,6 +184,11 @@ test_expect_success 'word diff with a regular expression' '
184 word_diff --color-words="[a-z]+"
185 '
186
187 +test_expect_success 'word diff with zero length matches' '
188 + cp expect.letter-runs-are-words expect &&
189 + word_diff --color-words="[a-z${LF}]*"
190 +'
191 +
192 test_expect_success 'set up a diff driver' '
193 git config diff.testdriver.wordRegex "[^[:space:]]" &&
194 cat <<-\EOF >.gitattributes