# [PATCH] submodule--helper: replace malloc with xmalloc

5 messages from 2026-03-10 to 2026-03-10. Participants: Siddharth Shrimali, Patrick Steinhardt, Junio C Hamano.
Thread: https://gitlist.dev/t/65198

## Siddharth Shrimali, 2026-03-10 12:10

Subject: [PATCH] submodule--helper: replace malloc with xmalloc
Message-ID: <20260310121013.39291-1-r.siddharth.shrimali@gmail.com>
URL: https://gitlist.dev/e/20260310121013.39291-1-r.siddharth.shrimali%40gmail.com

```
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.

Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
---
 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;
-- 
2.51.2


```

## Patrick Steinhardt, 2026-03-10 12:22

Subject: Re: [PATCH] submodule--helper: replace malloc with xmalloc
Message-ID: <abAM68lOLFVwmN5Y@pks.im>
URL: https://gitlist.dev/e/abAM68lOLFVwmN5Y%40pks.im
In-Reply-To: <20260310121013.39291-1-r.siddharth.shrimali@gmail.com>

```
On Tue, Mar 10, 2026 at 05:40:13PM +0530, Siddharth Shrimali wrote:
> 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.
> 
> Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
> ---
>  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;

Yup, looks good to me, and all the other while-at-it changes are
sensible, as well. Thanks!

Patrick

```

## Junio C Hamano, 2026-03-10 16:03

Subject: Re: [PATCH] submodule--helper: replace malloc with xmalloc
Message-ID: <xmqqqzprwu1q.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqqzprwu1q.fsf%40gitster.g
In-Reply-To: <20260310121013.39291-1-r.siddharth.shrimali@gmail.com>

```
Siddharth Shrimali <r.siddharth.shrimali@gmail.com> 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 <r.siddharth.shrimali@gmail.com>
> ---
>  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;

```

## Siddharth Shrimali, 2026-03-10 16:44

Subject: [PATCH v2] submodule--helper: replace malloc with xmalloc
Message-ID: <20260310164412.47403-1-r.siddharth.shrimali@gmail.com>
URL: https://gitlist.dev/e/20260310164412.47403-1-r.siddharth.shrimali%40gmail.com
In-Reply-To: <xmqqqzprwu1q.fsf@gitster.g>

```
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, drop the explicit type cast. In C, a void pointer (as
returned by xmalloc) is automatically promoted to the destination
pointer type. Removing the cast removes redundant syntax and prevents
potential bugs by ensuring the allocation stays synchronized with
the variable type if the declaration of 'temp' changes in the future.

Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
---
Changes in V2:
- Improved the commit message to explain the reasoning for removing
  the explicit type cast as requested by Junio.

 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;
-- 
2.51.2


```

## Junio C Hamano, 2026-03-10 19:35

Subject: Re: [PATCH v2] submodule--helper: replace malloc with xmalloc
Message-ID: <xmqqo6kvtr4m.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqo6kvtr4m.fsf%40gitster.g
In-Reply-To: <20260310164412.47403-1-r.siddharth.shrimali@gmail.com>

```
Siddharth Shrimali <r.siddharth.shrimali@gmail.com> 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.

```
