git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH/RFC] Disallow NULL in struct commit_list
@ 2011-08-17  2:01 Nguyễn Thái Ngọc Duy
  0 siblings, 0 replies; only message in thread
From: Nguyễn Thái Ngọc Duy @ 2011-08-17  2:01 UTC (permalink / raw)
  To: git; +Cc: Nguyễn Thái Ngọc Duy

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2011-08-17  2:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-17  2:01 [PATCH/RFC] Disallow NULL in struct commit_list Nguyễn Thái Ngọc Duy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).