apply: carefully strdup a possibly-NULL name
authorThomas Rast <trast@inf.ethz.ch>
Fri, 21 Jun 2013 11:38:00 +0000 (13:38 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Jun 2013 15:36:07 +0000 (08:36 -0700)
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>
builtin/apply.c
t/t4111-apply-subdir.sh
index b4428ea34f53d94e3733796777866e73531f06b5..f69a864abe8073b60a0cef743e98d249e3005796 100644 (file)
@@ -898,7 +898,7 @@ static void parse_traditional_patch(const char *first, const char *second, struc
                        patch->old_name = name;
                } else {
                        patch->old_name = name;
-                       patch->new_name = xstrdup(name);
+                       patch->new_name = null_strdup(name);
                }
        }
        if (!name)
index 7c398432bad761c1b38c6d15548bed0c389221fd..1618a6dbc7c718677cf669b5f6fc96f2682992fb 100755 (executable)
@@ -86,6 +86,20 @@ test_expect_success 'apply --index from subdir of toplevel' '
        test_cmp expected sub/dir/file
 '
 
+test_expect_success 'apply half-broken patch from subdir of toplevel' '
+       (
+               cd sub/dir &&
+               test_must_fail git apply <<-EOF
+               --- sub/dir/file
+               +++ sub/dir/file
+               @@ -1,0 +1,0 @@
+               --- file_in_root
+               +++ file_in_root
+               @@ -1,0 +1,0 @@
+               EOF
+       )
+'
+
 test_expect_success 'apply from .git dir' '
        cp postimage expected &&
        cp preimage .git/file &&