json: fix off-by-one in memory alloc
[fio.git] / json.c
diff --git a/json.c b/json.c
index ea61af72c422ad575238eaef2f4b4b2153a471d2..cdc3b2185fa428e3a68842c44b7079448607108b 100644 (file)
--- a/json.c
+++ b/json.c
@@ -63,18 +63,22 @@ static char *strdup_escape(const char *str)
        char *p, *ret;
        int escapes;
 
+       if (!strlen(str))
+               return NULL;
+
        escapes = 0;
        while ((input = strpbrk(input, "\\\"")) != NULL) {
                escapes++;
                input++;
        }
 
-       p = ret = malloc(strlen(str) + escapes);
+       p = ret = malloc(strlen(str) + escapes + 1);
        while (*str) {
                if (*str == '\\' || *str == '\"')
                        *p++ = '\\';
                *p++ = *str++;
        }
+       *p = '\0';
 
        return ret;
 }