apply: check git diffs for missing old filenames

2c93286a (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 <vegard.nossum@oracle.com> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 27, 2017 at 19:03 UTC 4269974179ff4fc2a970c972330ba5b7f26a323b
2 files changed +26 -1
apply.c
+2 -1
@@ -1585,7 +1585,8 @@ static int find_header(struct apply_state *state,
1585 patch->old_name = xstrdup(patch->def_name);
1586 patch->new_name = xstrdup(patch->def_name);
1587 }
1588 - if (!patch->is_delete && !patch->new_name) {
1588 + if ((!patch->new_name && !patch->is_delete) ||
1589 + (!patch->old_name && !patch->is_new)) {
1590 error(_("git diff header lacks filename information "
1591 "(line %d)"), state->linenr);
1592 return -128;
t/t4133-apply-filenames.sh
+24
@@ -35,4 +35,28 @@ test_expect_success 'apply diff with inconsistent filenames in headers' '
35 test_i18ngrep "inconsistent old filename" err
36 '
37
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 +
62 test_done