From 212eb96a96e256933a76e48d85fa4f500bed0dee Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Fri, 21 Jun 2013 13:38:00 +0200 Subject: [PATCH] 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 Signed-off-by: Junio C Hamano --- builtin/apply.c | 2 +- t/t4111-apply-subdir.sh | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/builtin/apply.c b/builtin/apply.c index b4428ea34..f69a864ab 100644 --- a/builtin/apply.c +++ b/builtin/apply.c @@ -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) diff --git a/t/t4111-apply-subdir.sh b/t/t4111-apply-subdir.sh index 7c398432b..1618a6dbc 100755 --- a/t/t4111-apply-subdir.sh +++ b/t/t4111-apply-subdir.sh @@ -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 && -- 2.11.0