# [PATCH] path: use xmalloc in add_to_trie

3 messages from 2017-10-24 to 2017-10-24. Participants: Andrey Okoshkin, Stefan Beller, Jeff King.
Thread: https://gitlist.dev/t/47019

## Andrey Okoshkin, 2017-10-24 15:15

Subject: [PATCH] path: use xmalloc in add_to_trie
Message-ID: <e63c2208-900e-138a-a13c-fecf9403e0b8@samsung.com>
URL: https://gitlist.dev/e/e63c2208-900e-138a-a13c-fecf9403e0b8%40samsung.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

```

## Stefan Beller, 2017-10-24 16:13

Subject: Re: [PATCH] path: use xmalloc in add_to_trie
Message-ID: <CAGZ79kZM_hVi8BeTmL5u_QK5+=1g3gcPsccEe1AJVpDLUWz_9Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kZM_hVi8BeTmL5u_QK5%2B%3D1g3gcPsccEe1AJVpDLUWz_9Q%40mail.gmail.com
In-Reply-To: <e63c2208-900e-138a-a13c-fecf9403e0b8@samsung.com>

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

Thanks,
Stefan

```

## Jeff King, 2017-10-24 19:26

Subject: Re: [PATCH] path: use xmalloc in add_to_trie
Message-ID: <20171024192658.4rvxvb34y64lt2tc@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20171024192658.4rvxvb34y64lt2tc%40sigill.intra.peff.net
In-Reply-To: <CAGZ79kZM_hVi8BeTmL5u_QK5+=1g3gcPsccEe1AJVpDLUWz_9Q@mail.gmail.com>

```
On Tue, Oct 24, 2017 at 09:13:28AM -0700, Stefan Beller wrote:

> 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

```
