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

[PATCH] path: use xmalloc in add_to_trie

From
Andrey Okoshkin <a.okoshkin@samsung.com>
Date
Oct 24, 2017, 15:15 UTC
Message-ID
<e63c2208-900e-138a-a13c-fecf9403e0b8@samsung.com>
In-Reply-To
<CGME20171024151508epcas2p4d9455e93e2e19bfb118b485d4ce14922@epcas2p4.samsung.com>

Add usage of xmalloc() instead of malloc() in add_to_trie() as xmalloc wraps and checks memory allocation result.

Signed-off-by: Andrey Okoshkin <a.okoshkin@samsung.com>
---
Hello,
I'm not sure but it looks like there is a missing check of the malloc result.
memcpy() may crash with SIGSEGV due to the memory allocation failure.
make_trie_node() uses xmalloc() and xcalloc() - so I believe add_to_trie()
also should use it.

Best regards, Andrey

 path.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/path.c b/path.c
index 335d4dd87..69b23ef17 100644
--- a/path.c
+++ b/path.c
@@ -191,7 +191,7 @@ static void *add_to_trie(struct trie *root, const char *key, void *value)
 		 * Split this node: child will contain this node's
 		 * existing children.
 		 */
-		child = malloc(sizeof(*child));
+		child = xmalloc(sizeof(*child));
 		memcpy(child->children, root->children, sizeof(root->children));
 
 		child->len = root->len - i - 1;
-- 
2.14.3
Next: Stefan Beller
Message 1 of 3 in “path: use xmalloc in add_to_trie”
  1. path: use xmalloc in add_to_trieAndrey Okoshkin, Oct 24, 2017
  2. Stefan BellerOct 24, 2017
  3. Jeff KingOct 24, 2017

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.