From: Jens Axboe Date: Tue, 13 Nov 2012 03:52:10 +0000 (-0700) Subject: JSON: fix escape of '"' and '\' characters X-Git-Tag: fio-2.0.11~13 X-Git-Url: https://git.kernel.dk/?p=fio.git;a=commitdiff_plain;h=d366a95e3c140e628efbac6448605455d8bd2dfc JSON: fix escape of '"' and '\' characters Reported-by: Kepler Kramer Signed-off-by: Jens Axboe --- diff --git a/json.c b/json.c index 8efbbdaf..ea61af72 100644 --- a/json.c +++ b/json.c @@ -57,13 +57,38 @@ 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; + + escapes = 0; + while ((input = strpbrk(input, "\\\"")) != NULL) { + escapes++; + input++; + } + + p = ret = malloc(strlen(str) + escapes); + while (*str) { + if (*str == '\\' || *str == '\"') + *p++ = '\\'; + *p++ = *str++; + } + + 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;