X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=json.c;h=cdc3b2185fa428e3a68842c44b7079448607108b;hp=8efbbdaf1cf1c5f89affa895689faaea0c574df6;hb=43f09da1e2c0fa04707e2bda4a361118091532c6;hpb=f3afa57e36550288340f1b6c694f354ae72654b9 diff --git a/json.c b/json.c index 8efbbdaf..cdc3b218 100644 --- a/json.c +++ b/json.c @@ -57,13 +57,42 @@ static struct json_value *json_create_value_float(float number) return value; } +static char *strdup_escape(const char *str) +{ + const char *input = 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 + 1); + while (*str) { + if (*str == '\\' || *str == '\"') + *p++ = '\\'; + *p++ = *str++; + } + *p = '\0'; + + return ret; +} + +/* + * Valid JSON strings must escape '"' and '/' with a preceeding '/' + */ static struct json_value *json_create_value_string(const char *str) { struct json_value *value = malloc(sizeof(struct json_value)); if (value) { value->type = JSON_TYPE_STRING; - value->string = strdup(str); + value->string = strdup_escape(str); if (!value->string) { free(value); value = NULL;