summaryrefslogtreecommitdiff
path: root/diff.c
diff options
context:
space:
mode:
authorThomas Rast <trast@student.ethz.ch>2013-02-26 20:47:01 (GMT)
committerJunio C Hamano <gitster@pobox.com>2013-02-26 21:01:34 (GMT)
commitdd281f09b7eab86e4983b37d011cbfb0f593f6b8 (patch)
tree3e1149754a47b97d47407c049d3a1f50fd8f8e1a /diff.c
parentd020e27fdaeb7d04247cdf5a0ff84d97626d5f5a (diff)
downloadgit-dd281f09b7eab86e4983b37d011cbfb0f593f6b8.zip
git-dd281f09b7eab86e4983b37d011cbfb0f593f6b8.tar.gz
git-dd281f09b7eab86e4983b37d011cbfb0f593f6b8.tar.bz2
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>
Diffstat (limited to 'diff.c')
-rw-r--r--diff.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/diff.c b/diff.c
index e1d82c9..d641c26 100644
--- a/diff.c
+++ b/diff.c
@@ -1151,6 +1151,7 @@ static char *pprint_rename(const char *a, const char *b)
const char *new = b;
struct strbuf name = STRBUF_INIT;
int pfx_length, sfx_length;
+ int pfx_adjust_for_slash;
int len_a = strlen(a);
int len_b = strlen(b);
int a_midlen, b_midlen;
@@ -1178,14 +1179,16 @@ static char *pprint_rename(const char *a, const char *b)
new = b + len_b;
sfx_length = 0;
/*
- * Note:
- * if pfx_length is 0, old/new will never reach a - 1 because it
- * would mean the whole string is common suffix. But then, the
- * whole string would also be a common prefix, and we would not
- * have pfx_length equals 0.
+ * If there is a common prefix, it must end in a slash. In
+ * that case we let this loop run 1 into the prefix to see the
+ * same slash.
+ *
+ * If there is no common prefix, we cannot do this as it would
+ * underrun the input strings.
*/
- while (a + pfx_length - 1 <= old &&
- b + pfx_length - 1 <= new &&
+ pfx_adjust_for_slash = (pfx_length ? 1 : 0);
+ while (a + pfx_length - pfx_adjust_for_slash <= old &&
+ b + pfx_length - pfx_adjust_for_slash <= new &&
*old == *new) {
if (*old == '/')
sfx_length = len_a - (old - a);