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 <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Rast committed
Feb 26, 2013 at 21:47 UTC
dd281f09b7eab86e4983b37d011cbfb0f593f6b8
1 file changed
+10
-7
diff.c
+10
-7
@@ -1151,6 +1151,7 @@ static char *pprint_rename(const char *a, const char *b)
1151
const char *new = b;
1152
struct strbuf name = STRBUF_INIT;
1153
int pfx_length, sfx_length;
1154
+ int pfx_adjust_for_slash;
1155
int len_a = strlen(a);
1156
int len_b = strlen(b);
1157
int a_midlen, b_midlen;
@@ -1178,14 +1179,16 @@ static char *pprint_rename(const char *a, const char *b)
1179
new = b + len_b;
1180
sfx_length = 0;
1181
/*
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.
1188
*/
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 &&
1192
*old == *new) {
1193
if (*old == '/')
1194
sfx_length = len_a - (old - a);