Disallow NULL in struct commit_list
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 <[email protected]>
--- builtin/merge.c | 2 +- builtin/revert.c | 2 +- commit.c | 3 +++ commit.h | 2 ++ 4 files changed, 7 insertions(+), 2 deletions(-)
builtin/merge.c +1 -1
@@ -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 ;
builtin/revert.c +1 -1
@@ -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;
commit.c +3 -0
@@ -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;
commit.h +2 -0
@@ -21,6 +21,8 @@ struct commit {char *buffer; }; +extern struct commit null_commit; +extern int save_commit_buffer;extern const char *commit_type;
Signature · 1 line
1.7.4.74.g639db