str: Make ss_alloc_substring() allocate null-terminated strings.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 11 Sep 2010 04:07:00 +0000 (21:07 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Fri, 24 Sep 2010 03:28:16 +0000 (20:28 -0700)
This has little cost and it is occasionally valuable.

src/libpspp/str.c

index bde4de4be5bb4ee13edf877e217537336debfc0a..9f0cb6e31463ab5caf319114e43fed26f640ae2b 100644 (file)
@@ -30,6 +30,7 @@
 #include <relocatable.h>
 #include "minmax.h"
 #include "xalloc.h"
+#include "xmemdup0.h"
 #include "xsize.h"
 \f
 /* Reverses the order of NBYTES bytes at address P, thus converting
@@ -379,14 +380,13 @@ ss_tail (struct substring ss, size_t cnt)
     return ss;
 }
 
-/* Makes a malloc()'d copy of the contents of OLD
+/* Makes a malloc()'d, null-terminated copy of the contents of OLD
    and stores it in NEW. */
 void
 ss_alloc_substring (struct substring *new, struct substring old)
 {
-  new->string = xmalloc (old.length);
+  new->string = xmemdup0 (old.string, old.length);
   new->length = old.length;
-  memcpy (new->string, old.string, old.length);
 }
 
 /* Allocates room for a CNT-character string in NEW. */