Skip to content

Commit 4269974

Browse files
rscharfegitster
authored andcommitted
apply: check git diffs for missing old filenames
2c93286 (fix "git apply --index ..." not to deref NULL) added a check for git patches missing a +++ line, preventing a segfault. Check for missing --- lines as well, and add a test for each case. Found by Vegard Nossum using AFL. Original-patch-by: Vegard Nossum <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 773e3a2 commit 4269974

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

apply.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,8 @@ static int find_header(struct apply_state *state,
15851585
patch->old_name = xstrdup(patch->def_name);
15861586
patch->new_name = xstrdup(patch->def_name);
15871587
}
1588-
if (!patch->is_delete && !patch->new_name) {
1588+
if ((!patch->new_name && !patch->is_delete) ||
1589+
(!patch->old_name && !patch->is_new)) {
15891590
error(_("git diff header lacks filename information "
15901591
"(line %d)"), state->linenr);
15911592
return -128;

t/t4133-apply-filenames.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,28 @@ test_expect_success 'apply diff with inconsistent filenames in headers' '
3535
test_i18ngrep "inconsistent old filename" err
3636
'
3737

38+
test_expect_success 'apply diff with new filename missing from headers' '
39+
cat >missing_new_filename.diff <<-\EOF &&
40+
diff --git a/f b/f
41+
index 0000000..d00491f
42+
--- a/f
43+
@@ -0,0 +1 @@
44+
+1
45+
EOF
46+
test_must_fail git apply missing_new_filename.diff 2>err &&
47+
test_i18ngrep "lacks filename information" err
48+
'
49+
50+
test_expect_success 'apply diff with old filename missing from headers' '
51+
cat >missing_old_filename.diff <<-\EOF &&
52+
diff --git a/f b/f
53+
index d00491f..0000000
54+
+++ b/f
55+
@@ -1 +0,0 @@
56+
-1
57+
EOF
58+
test_must_fail git apply missing_old_filename.diff 2>err &&
59+
test_i18ngrep "lacks filename information" err
60+
'
61+
3862
test_done

0 commit comments

Comments
 (0)