From: Ben Pfaff Date: Wed, 23 Mar 2011 03:55:55 +0000 (-0700) Subject: str: Make ss_alloc_substring_pool() null-terminate its output. X-Git-Tag: v0.7.7~4 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pspp-builds.git;a=commitdiff_plain;h=03bfb72314e434ddaa2ff2b770d1d9df2ea1927a str: Make ss_alloc_substring_pool() null-terminate its output. It's inconsistent that ss_alloc_substring() null-terminates its output but ss_alloc_substring_pool() does not. This caught us out in recode_substring_pool(), which used ss_alloc_substring_pool() in a fallback case where create_iconv() failed and expected the result to be null-terminated. Reported-by: Jeremy Lavergne --- diff --git a/src/libpspp/str.c b/src/libpspp/str.c index ba4a26f1..08a85ad7 100644 --- a/src/libpspp/str.c +++ b/src/libpspp/str.c @@ -347,15 +347,16 @@ ss_realloc (struct substring *ss, size_t size) ss->string = xrealloc (ss->string, size); } -/* Makes a pool_alloc_unaligned()'d copy of the contents of OLD - in POOL, and stores it in NEW. */ +/* Makes a pool_alloc_unaligned()'d, null-terminated copy of the contents of + OLD in POOL, and stores it in NEW. */ void ss_alloc_substring_pool (struct substring *new, struct substring old, struct pool *pool) { - new->string = pool_alloc_unaligned (pool, old.length); + new->string = pool_alloc_unaligned (pool, old.length + 1); new->length = old.length; memcpy (new->string, old.string, old.length); + new->string[old.length] = '\0'; } /* Allocates room for a CNT-byte string in NEW in POOL. */