git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] apply: carefully strdup a possibly-NULL name

From
Thomas Rast <trast@inf.ethz.ch>
Date
Jun 21, 2013, 11:38 UTC
Message-ID
<76659b15f0a52d9e94143bee803f5cf45de1e6cc.1371814540.git.trast@inf.ethz.ch>

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>
---
 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 faf8e30..0e9b631 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -906,7 +906,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 7c39843..1618a6d 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 &&
-- 
1.8.3.1.664.gae9f72a
Message 1 of 1 in “apply: carefully strdup a possibly-NULL name”
  1. apply: carefully strdup a possibly-NULL nameThomas Rast, Jun 21, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.