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

[PATCH/RFC] Disallow NULL in struct commit_list

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Aug 17, 2011, 02:01 UTC
Message-ID
<1313546478-8833-1-git-send-email-pclouds@gmail.com>

It seems to me that in most cases commit_list is supposed to contain real commits. If someone puts a NULL there by accident, it'd be hard to track down because sigsegv would happen later when the the commit is used, not when added.

So perhaps we could safeguard commit_list_insert(). If a bug tries to put NULL in, it'd be caught earlier.

There is code that add NULL commit on purpose, "make test" only catches one in revert.c, but I may miss other cases and crash system unnecessarily.

Not sure if this patch is worth the trouble. Maybe make it permanent resident of next and never graduate to master?

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/merge.c  |    2 +-
 builtin/revert.c |    2 +-
 commit.c         |    3 +++
 commit.h         |    2 ++
 4 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index 325891e..158008d 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -611,7 +611,7 @@ static void write_tree_trivial(unsigned char *sha1)
 
 static const char *merge_argument(struct commit *commit)
 {
-	if (commit)
+	if (commit && commit != &null_commit)
 		return sha1_to_hex(commit->object.sha1);
 	else
 		return EMPTY_TREE_SHA1_HEX;
diff --git a/builtin/revert.c b/builtin/revert.c
index 1f27c63..7d5005e 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -471,7 +471,7 @@ static int do_pick_commit(void)
 		}
 		strbuf_addstr(&msgbuf, ".\n");
 	} else {
-		base = parent;
+		base = parent ? parent : &null_commit;
 		base_label = msg.parent_label;
 		next = commit;
 		next_label = msg.label;
diff --git a/commit.c b/commit.c
index dc22695..604d2f1 100644
--- a/commit.c
+++ b/commit.c
@@ -11,6 +11,8 @@ int save_commit_buffer = 1;
 
 const char *commit_type = "commit";
 
+struct commit null_commit;
+
 static struct commit *check_commit(struct object *obj,
 				   const unsigned char *sha1,
 				   int quiet)
@@ -363,6 +365,7 @@ int find_commit_subject(const char *commit_buffer, const char **subject)
 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 {
 	struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
+	assert(item != NULL);
 	new_list->item = item;
 	new_list->next = *list_p;
 	*list_p = new_list;
diff --git a/commit.h b/commit.h
index 0e36fd0..c9f0743 100644
--- a/commit.h
+++ b/commit.h
@@ -21,6 +21,8 @@ struct commit {
 	char *buffer;
 };
 
+extern struct commit null_commit;
+
 extern int save_commit_buffer;
 extern const char *commit_type;
 
-- 
1.7.4.74.g639db
Message 1 of 1 in “Disallow NULL in struct commit_list”
  1. Disallow NULL in struct commit_listNguyễn Thái Ngọc Duy, Aug 17, 2011

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.