X-Git-Url: https://git.kernel.dk/?a=blobdiff_plain;f=json.c;h=cba370ab4bdc59f70c425f9cea38fd63f4e47829;hb=1417daeb92c44632f2e1e376d736a0f198d7af8d;hp=8efbbdaf1cf1c5f89affa895689faaea0c574df6;hpb=d79db1222039e906dd49ae290daa59701f4e2385;p=fio.git diff --git a/json.c b/json.c index 8efbbdaf..cba370ab 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 preceding '/' + */ 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;