[PATCH/RFC] Disallow NULL in struct commit_list

From: Nguyễn Thái Ngọc Duy <hidden>
Date: 2016-06-15 22:51:49
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help