Re: [PATCH 1/6] path.c: allocate enough memory for string
Stefan Beller writes: > On Tue, Mar 29, 2016 at 5:57 PM, Eric Sunshine > wrote: >> On Tue, Mar 29, 2016 at 8:38 PM, Stefan Beller wrote: >>> `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 >>> --- >>> diff --git 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); >> >> Huh? This is a trie. It never accesses 'contents' as a NUL-terminated >> string. Plus, no NUL is ever even copied, thus this is just >> overallocating. How is this an improvement? > > By using strlen, I assumed it was a standard C string. > I missed that, though. You took hint from a wrong place. You are auditing the destination buffer, so the correct place to take hint from is the memcpy() that touches the destination. -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/6] path.c: allocate enough memory for string
On Tue, Mar 29, 2016 at 5:57 PM, Eric Sunshine wrote: > On Tue, Mar 29, 2016 at 8:38 PM, Stefan Beller wrote: >> `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 >> --- >> diff --git 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); > > Huh? This is a trie. It never accesses 'contents' as a NUL-terminated > string. Plus, no NUL is ever even copied, thus this is just > overallocating. How is this an improvement? By using strlen, I assumed it was a standard C string. I missed that, though. > >> } >> new_node->value = value; >> -- -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/6] path.c: allocate enough memory for string
Stefan Beller writes: > `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 > --- > 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); > } This structure looks like a counted string that does not want to have a terminating NUL after the contents, judging from the way memcpy() copies only len and not len+1. Did I write this (wondering why this was addressed to me)? > new_node->value = value; -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/6] path.c: allocate enough memory for string
On Tue, Mar 29, 2016 at 8:38 PM, Stefan Beller wrote: > `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 > --- > diff --git 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); Huh? This is a trie. It never accesses 'contents' as a NUL-terminated string. Plus, no NUL is ever even copied, thus this is just overallocating. How is this an improvement? > } > new_node->value = value; > -- -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majord...@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html