git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
Date
Mar 10, 2026, 16:44 UTC
Message-ID
<20260310164412.47403-1-r.siddharth.shrimali@gmail.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
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 5 in “submodule--helper: replace malloc with xmalloc”
  1. submodule--helper: replace malloc with xmallocSiddharth Shrimali, Mar 10, 2026
  2. Patrick SteinhardtMar 10, 2026
  3. Junio C HamanoMar 10, 2026
  4. submodule--helper: replace malloc with xmallocSiddharth Shrimali, Mar 10, 2026
  5. Junio C HamanoMar 10, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.