From 03bfb72314e434ddaa2ff2b770d1d9df2ea1927a Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@cs.stanford.edu>
Date: Tue, 22 Mar 2011 20:55:55 -0700
Subject: [PATCH] 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 <jeremy@lavergne.gotdns.org>
---
 src/libpspp/str.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/libpspp/str.c b/src/libpspp/str.c
index ba4a26f142..08a85ad790 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. */
-- 
2.30.2