Skip to content

Commit 85a9a63

Browse files
peffgitster
authored andcommitted
diff: handle NULL meta-info when spawning external diff
Running this: $ touch foo bar $ chmod +x foo $ git -c diff.external=echo diff --ext-diff --no-index foo bar results in a segfault. The issue is that run_diff_cmd() passes a NULL "xfrm_msg" variable to run_external_diff(), which feeds it to strvec_push(), causing the segfault. The bug dates back to 82fbf26 (run_external_diff: use an argv_array for the command line, 2014-04-19), though it mostly only ever worked accidentally. Before then, we just stuck the NULL pointer into a "const char **" array, so our NULL ended up acting as an extra end-of-argv sentinel (which was OK, because it was the last thing in the array). Curiously, though, this is only a problem with --no-index. We set up xfrm_msg by calling fill_metainfo(). This result may be empty, or may have text like "index 1234..5678\n", "rename from foo\nrename from bar\n", etc. In run_external_diff(), we only look at xfrm_msg if the "other" variable is not NULL. That variable is set when the paths of the two sides of the diff pair aren't the same (in which case the destination path becomes "other"). So normally it would kick in only for a rename, in which case xfrm_msg should not be NULL (it would have the rename information in it). But with a "--no-index" of two blobs, we of course have two different pathnames, and thus end up with a non-NULL "other" filename (which is always just a repeat of the file2-name), but possibly a NULL xfrm_msg. So how to fix it? I have a feeling that --no-index always passing "other" to the external diff command is probably a bug. There was no rename, and the name is always redundant with existing information we pass (and this may even cause us to pass a useless "xfrm_msg" that contains an "index 1234..5678" line). So one option would be to change that behavior. We don't seem to have ever documented the "other" or "xfrm_msg" parameters for external diffs. But I'm not sure what fallout we might have from changing that behavior now. So this patch takes the less-risky option, and simply teaches run_external_diff() to avoid passing xfrm_msg when it's NULL. That makes it agnostic to whether "other" and "xfrm_msg" always come as a pair. It fixes the segfault now, and if we want to change the --no-index "other" behavior on top, it will handle that, too. Reported-by: Wilfred Hughes <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 564d025 commit 85a9a63

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

diff.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4369,7 +4369,8 @@ static void run_external_diff(const char *pgm,
43694369
add_external_diff_name(o->repo, &cmd.args, two);
43704370
if (other) {
43714371
strvec_push(&cmd.args, other);
4372-
strvec_push(&cmd.args, xfrm_msg);
4372+
if (xfrm_msg)
4373+
strvec_push(&cmd.args, xfrm_msg);
43734374
}
43744375
}
43754376

t/t4053-diff-no-index.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,18 @@ test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not lik
205205
test_cmp expected actual
206206
'
207207

208+
test_expect_success POSIXPERM 'external diff with mode-only change' '
209+
echo content >not-executable &&
210+
echo content >executable &&
211+
chmod +x executable &&
212+
echo executable executable $(test_oid zero) 100755 \
213+
not-executable $(test_oid zero) 100644 not-executable \
214+
>expect &&
215+
test_expect_code 1 git -c diff.external=echo diff \
216+
--no-index executable not-executable >actual &&
217+
test_cmp expect actual
218+
'
219+
208220
test_expect_success "diff --no-index treats '-' as stdin" '
209221
cat >expect <<-EOF &&
210222
diff --git a/- b/a/1

0 commit comments

Comments
 (0)