json: fix off-by-one in memory alloc
Tighten it a bit too, checking for empty string.
Signed-off-by: Jens Axboe <axboe@kernel.dk>
diff --git a/json.c b/json.c
index ea61af7..cdc3b21 100644
--- a/json.c
+++ b/json.c
@@ -63,18 +63,22 @@
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;
}