threads / patch / 47019

patchpath: use xmalloc in add_to_trie

Subject: [PATCH] path: use xmalloc in add_to_trie

## tl;dr

3 messages between Oct 24, 2017 and Oct 24, 2017. Diffs are folded; open one to read it.

replies: 2people: 3as markdown or json

Andrey Okoshkin· Oct 24, 2017, 15:15 UTC · lore

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(-)
Show changes to path.c +1 −1
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
Stefan Beller· Oct 24, 2017, 16:13 UTC · re: Andrey Okoshkin · lore

Re: [PATCH] path: use xmalloc in add_to_trie

On Tue, Oct 24, 2017 at 8:15 AM, Andrey Okoshkin <a.okoshkin@samsung.com> wrote:
Show 10 quoted lines
> 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.
Good catch! Thanks for spotting.

Trying to find similar occurrences via git grep "= malloc" did not yield other places that need the same fix.

Thanks, Stefan

Jeff King· Oct 24, 2017, 19:26 UTC · re: Stefan Beller · lore

Re: [PATCH] path: use xmalloc in add_to_trie

On Tue, Oct 24, 2017 at 09:13:28AM -0700, Stefan Beller wrote:
Show 16 quoted lines
> On Tue, Oct 24, 2017 at 8:15 AM, Andrey Okoshkin <a.okoshkin@samsung.com> wrote:
> > 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.
> 
> Good catch! Thanks for spotting.
> 
> Trying to find similar occurrences via git grep "= malloc" did not
> yield other places that need the same fix.

Don't forget realloc and calloc (though I don't think there are any cases that need touching there; just a hint for anybody doing auditing).

-Peff

← back to recent threads