Repository: lucy-clownfish Updated Branches: refs/heads/0.5 443645ab6 -> cf9d70341
Null-terminate buffer in CB_Yield_String This makes sure that strings created with CharBufs can safely be accessed as C strings. Fixes invalid reads in the Lucy test suite. Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/cf9d7034 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/cf9d7034 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/cf9d7034 Branch: refs/heads/0.5 Commit: cf9d703419a0351daf247d37355976c67499217a Parents: 443645a Author: Nick Wellnhofer <[email protected]> Authored: Wed Apr 6 16:25:23 2016 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Wed Apr 6 16:28:08 2016 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/CharBuf.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/cf9d7034/runtime/core/Clownfish/CharBuf.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/CharBuf.c b/runtime/core/Clownfish/CharBuf.c index 53541c2..665a935 100644 --- a/runtime/core/Clownfish/CharBuf.c +++ b/runtime/core/Clownfish/CharBuf.c @@ -274,11 +274,18 @@ CB_To_String_IMP(CharBuf *self) { String* CB_Yield_String_IMP(CharBuf *self) { - String *retval - = Str_new_steal_trusted_utf8(self->ptr, self->size); + // Null-terminate buffer. + size_t size = self->size; + SI_add_grow_and_oversize(self, size, 1); + self->ptr[size] = '\0'; + + String *retval = Str_new_steal_trusted_utf8(self->ptr, size); + + // Clear CharBuf. self->ptr = NULL; self->size = 0; self->cap = 0; + return retval; }
