Subject: Re: [PATCH v2] submodule--helper: replace malloc with xmalloc

From: Junio C Hamano <[email-masked]>
Date: Tue, 10 Mar 2026 12:35:05 -0700

Siddharth Shrimali <[email-masked]> writes:

> The submodule_summary_callback() function currently uses a raw malloc()
> which could lead to a NULL pointer dereference.
>
> Standardize this by replacing malloc() with xmalloc() for error handling.
> To improve maintainability, use sizeof(*temp) instead of the struct name.
>
> While at it, ...

I think use of sizeof(*temp) and dropping of a cast from (void *)
fall into the same bucket, i.e. to improve maintainability.  Both
are good changes.

> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 143f7cb3cc..f3e132888f 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -1160,7 +1160,7 @@ static void submodule_summary_callback(struct diff_queue_struct *q,
>  
>  		if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
>  			continue;
> -		temp = (struct module_cb*)malloc(sizeof(struct module_cb));
> +		temp = xmalloc(sizeof(*temp));

Looking good.

Thanks.