apply: carefully strdup a possibly-NULL name

2901bbe (apply: free patch->{def,old,new}_name fields, 2012-03-21) cleaned up the memory management of filenames in the patches, but forgot that find_name_traditional() can return NULL as a way of saying "I couldn't find a name". That NULL unfortunately gets passed into xstrdup() next, resulting in a segfault. Use null_strdup() so as to safely propagate the null, which will let us emit the correct error message. Reported-by: DevHC on #git Signed-off-by: Thomas Rast <trast@inf.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Jun 21, 2013 at 13:38 UTC 212eb96a96e256933a76e48d85fa4f500bed0dee
2 files changed +15 -1
builtin/apply.c
+1 -1
@@ -898,7 +898,7 @@ static void parse_traditional_patch(const char *first, const char *second, struc
898 patch->old_name = name;
899 } else {
900 patch->old_name = name;
901 - patch->new_name = xstrdup(name);
901 + patch->new_name = null_strdup(name);
902 }
903 }
904 if (!name)
t/t4111-apply-subdir.sh
+14
@@ -86,6 +86,20 @@ test_expect_success 'apply --index from subdir of toplevel' '
86 test_cmp expected sub/dir/file
87 '
88
89 +test_expect_success 'apply half-broken patch from subdir of toplevel' '
90 + (
91 + cd sub/dir &&
92 + test_must_fail git apply <<-EOF
93 + --- sub/dir/file
94 + +++ sub/dir/file
95 + @@ -1,0 +1,0 @@
96 + --- file_in_root
97 + +++ file_in_root
98 + @@ -1,0 +1,0 @@
99 + EOF
100 + )
101 +'
102 +
103 test_expect_success 'apply from .git dir' '
104 cp postimage expected &&
105 cp preimage .git/file &&