Re: [PATCH] list-objects: perform NULL check before the variable is dereferenced
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Apr 9, 2012, 18:51 UTC
- Message-ID
- <7v7gxowxfh.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <1333968311-29889-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> > --- > list-objects.c | 10 ++++++---- > 1 files changed, 6 insertions(+), 4 deletions(-)
Please explain why this is needed?
I can see that process_blob() is called from process_tree() which passes the return value from lookup_blob(entry.sha1) directly without looking at it. lookup_blob() can issue an error message and return NULL if there is a SHA-1 collision with an object that is not a blob.
Show 40 quoted lines
> diff --git a/list-objects.c b/list-objects.c
> index 3dd4a96..34044d9 100644
> --- a/list-objects.c
> +++ b/list-objects.c
> @@ -15,12 +15,13 @@ static void process_blob(struct rev_info *revs,
> const char *name,
> void *cb_data)
> {
> - struct object *obj = &blob->object;
> + struct object *obj;
>
> if (!revs->blob_objects)
> return;
> - if (!obj)
> + if (!blob)
> die("bad blob object");
> + obj = &blob->object;
> if (obj->flags & (UNINTERESTING | SEEN))
> return;
> obj->flags |= SEEN;
> @@ -67,7 +68,7 @@ static void process_tree(struct rev_info *revs,
> const char *name,
> void *cb_data)
> {
> - struct object *obj = &tree->object;
> + struct object *obj;
> struct tree_desc desc;
> struct name_entry entry;
> struct name_path me;
> @@ -77,8 +78,9 @@ static void process_tree(struct rev_info *revs,
>
> if (!revs->tree_objects)
> return;
> - if (!obj)
> + if (!tree)
> die("bad tree object");
> + obj = &tree->object;
> if (obj->flags & (UNINTERESTING | SEEN))
> return;
> if (parse_tree(tree) < 0)