From 2cb033e0d94a229810b110a49a5052ef1a4dc7db Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Sat, 14 Jun 2008 20:53:45 -0700 Subject: [PATCH] strfunc: always null-terminate the output buffer 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 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/strfunc.c b/strfunc.c index 9fb72706..ac56ac73 100644 --- 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); } -- 2.11.4.GIT