Skip to content

Commit dd281f0

Browse files
trastgitster
authored andcommitted
diff: prevent pprint_rename from underrunning input
The logic described in d020e27 (diff: Fix rename pretty-print when suffix and prefix overlap, 2013-02-23) is wrong: The proof in the comment is valid only if both strings are the same length. *One* of old/new can reach a-1 (b-1, resp.) if 'a' is a suffix of 'b' (or vice versa). Since the intent was to let the loop run down to the '/' at the end of the common prefix, fix it by making that distinction explicit: if there is no prefix, allow no underrun. Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d020e27 commit dd281f0

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

diff.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,7 @@ static char *pprint_rename(const char *a, const char *b)
11511151
const char *new = b;
11521152
struct strbuf name = STRBUF_INIT;
11531153
int pfx_length, sfx_length;
1154+
int pfx_adjust_for_slash;
11541155
int len_a = strlen(a);
11551156
int len_b = strlen(b);
11561157
int a_midlen, b_midlen;
@@ -1178,14 +1179,16 @@ static char *pprint_rename(const char *a, const char *b)
11781179
new = b + len_b;
11791180
sfx_length = 0;
11801181
/*
1181-
* Note:
1182-
* if pfx_length is 0, old/new will never reach a - 1 because it
1183-
* would mean the whole string is common suffix. But then, the
1184-
* whole string would also be a common prefix, and we would not
1185-
* have pfx_length equals 0.
1182+
* If there is a common prefix, it must end in a slash. In
1183+
* that case we let this loop run 1 into the prefix to see the
1184+
* same slash.
1185+
*
1186+
* If there is no common prefix, we cannot do this as it would
1187+
* underrun the input strings.
11861188
*/
1187-
while (a + pfx_length - 1 <= old &&
1188-
b + pfx_length - 1 <= new &&
1189+
pfx_adjust_for_slash = (pfx_length ? 1 : 0);
1190+
while (a + pfx_length - pfx_adjust_for_slash <= old &&
1191+
b + pfx_length - pfx_adjust_for_slash <= new &&
11891192
*old == *new) {
11901193
if (*old == '/')
11911194
sfx_length = len_a - (old - a);

0 commit comments

Comments
 (0)