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

From: Junio C Hamano <[email-masked]>
Date: Tue, 10 Mar 2026 09:03:45 -0700

Siddharth Shrimali <[email-masked]> writes:

> The submodule_summary_callback() function currently uses a raw malloc()
> which could lead to NULL pointer dereference.
>
> Standardize this by replacing malloc() with xmalloc() for error handling.
> Also, remove the unnecessary type cast and use sizeof(*temp) instead of
> struct name in xmalloc to improve maintainability of the code.

The proposed log message should explain why it is a good change to
lose the cast.

>
> Signed-off-by: Siddharth Shrimali <[email-masked]>
> ---
>  builtin/submodule--helper.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> 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));
>  		temp->mod_src = p->one->mode;
>  		temp->mod_dst = p->two->mode;
>  		temp->oid_src = p->one->oid;