public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Siddharth Shrimali <r.siddharth.shrimali@gmail•com>
To: git@vger•kernel.org
Cc: gitster@pobox•com, ps@pks•im, r.siddharth.shrimali@gmail•com
Subject: [PATCH v2] submodule--helper: replace malloc with xmalloc
Date: Tue, 10 Mar 2026 22:14:12 +0530	[thread overview]
Message-ID: <20260310164412.47403-1-r.siddharth.shrimali@gmail.com> (raw)
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


  reply	other threads:[~2026-03-10 16:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-10 12:10 [PATCH] submodule--helper: replace malloc with xmalloc Siddharth Shrimali
2026-03-10 12:22 ` Patrick Steinhardt
2026-03-10 16:03 ` Junio C Hamano
2026-03-10 16:44   ` Siddharth Shrimali [this message]
2026-03-10 19:35     ` [PATCH v2] " Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260310164412.47403-1-r.siddharth.shrimali@gmail.com \
    --to=r.siddharth.shrimali@gmail$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=gitster@pobox$(echo .)com \
    --cc=ps@pks$(echo .)im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox