[PATCH 1/6] path.c: allocate enough memory for string
- From
Stefan Beller <sbeller@google.com>
- Date
- Mar 30, 2016, 00:38 UTC
- Message-ID
- <1459298333-21899-2-git-send-email-sbeller@google.com>
- In-Reply-To
- <1459298333-21899-1-git-send-email-sbeller@google.com>
`strlen` returns the length of a string without the terminating null byte. To make sure enough memory is allocated we need to pass `strlen(..) + 1` to the allocation function.
Signed-off-by: Stefan Beller <sbeller@google.com> --- path.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/path.c b/path.c index 969b494..0ae8af5 100644 --- a/path.c +++ b/path.c @@ -155,7 +155,7 @@ static struct trie *make_trie_node(const char *key, void *value) struct trie *new_node = xcalloc(1, sizeof(*new_node)); new_node->len = strlen(key); if (new_node->len) { - new_node->contents = xmalloc(new_node->len); + new_node->contents = xmalloc(new_node->len + 1); memcpy(new_node->contents, key, new_node->len); } new_node->value = value;
-- 2.8.0.8.g27a27a6.dirty