X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=json.c;h=cdc3b2185fa428e3a68842c44b7079448607108b;hp=707ea141cba973e74455c41d3072b048f3b6f827;hb=35922a2138c5adc91a2b4b185b5d756900bde87d;hpb=cc372b17f2827e89da79241f1bbaca1e7c650611 diff --git a/json.c b/json.c index 707ea141..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; @@ -96,9 +125,9 @@ static struct json_value *json_create_value_array(struct json_array *array) return value; } -static void json_free_array(struct json_array *array); static void json_free_pair(struct json_pair *pair); static void json_free_value(struct json_value *value); + void json_free_object(struct json_object *obj) { int i;