strfunc: always null-terminate the output buffer
authorH. Peter Anvin <hpa@zytor.com>
Sun, 15 Jun 2008 03:53:45 +0000 (20:53 -0700)
committerH. Peter Anvin <hpa@zytor.com>
Sun, 15 Jun 2008 03:53:45 +0000 (20:53 -0700)
Make sure that the buffer is always null-terminated, even though we do
have to use the length, since the string can (and often will) have
embedded nulls.

strfunc.c

index 9fb7270..ac56ac7 100644 (file)
--- a/strfunc.c
+++ b/strfunc.c
@@ -158,10 +158,13 @@ size_t string_transform(char *str, size_t len, char **out, enum strfunc func)
     transform_func transform = str_transforms[func];
     size_t outlen;
     uint8_t *s = (uint8_t *)str;
+    char *buf;
 
     outlen = transform(s, len, NULL);
     if (outlen == (size_t)-1)
        return -1;
 
-    return transform(s, len, *out = nasm_malloc(outlen));
+    *out = buf = nasm_malloc(outlen+1);
+    buf[outlen] = '\0';                /* Forcibly null-terminate the buffer */
+    return transform(s, len, buf);
 }