From 4e8a9012423dd8dda6b8f7b4cae40cbd440011a8 Mon Sep 17 00:00:00 2001 From: Krzysztof Gabis Date: Sat, 16 Sep 2017 16:07:43 +0100 Subject: [PATCH] Changes float print format, removes array/object capacity limit, doesn't accept inf/nan numbers. --- parson.c | 30 ++++++++++-------------------- tests.c | 25 +++++++++++++++---------- tests/test_2.txt | 2 ++ tests/test_2_comments.txt | 2 ++ tests/test_2_pretty.txt | 6 ++++-- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/parson.c b/parson.c index 202b8fb..3f89b7f 100644 --- a/parson.c +++ b/parson.c @@ -39,11 +39,9 @@ * don't have to. */ #define sscanf THINK_TWICE_ABOUT_USING_SSCANF -#define STARTING_CAPACITY 15 -#define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */ -#define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */ -#define MAX_NESTING 2048 -#define DOUBLE_SERIALIZATION_FORMAT "%f" +#define STARTING_CAPACITY 16 +#define MAX_NESTING 2048 +#define FLOAT_FORMAT "%1.17g" #define SIZEOF_TOKEN(a) (sizeof(a) - 1) #define SKIP_CHAR(str) ((*str)++) @@ -354,9 +352,6 @@ static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_V } if (object->count >= object->capacity) { size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); - if (new_capacity > OBJECT_MAX_CAPACITY) { - return JSONFailure; - } if (json_object_resize(object, new_capacity) == JSONFailure) { return JSONFailure; } @@ -443,9 +438,6 @@ static JSON_Array * json_array_init(JSON_Value *wrapping_value) { static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) { if (array->count >= array->capacity) { size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); - if (new_capacity > ARRAY_MAX_CAPACITY) { - return JSONFailure; - } if (json_array_resize(array, new_capacity) == JSONFailure) { return JSONFailure; } @@ -929,13 +921,7 @@ static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int le if (buf != NULL) { num_buf = buf; } - if (num == ((double)(int)num)) { /* check if num is integer */ - written = sprintf(num_buf, "%d", (int)num); - } else if (num == ((double)(unsigned int)num)) { - written = sprintf(num_buf, "%u", (unsigned int)num); - } else { - written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num); - } + written = sprintf(num_buf, FLOAT_FORMAT, num); if (written < 0) { return -1; } @@ -1315,8 +1301,12 @@ JSON_Value * json_value_init_string(const char *string) { } JSON_Value * json_value_init_number(double number) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) { + JSON_Value *new_value = NULL; + if ((number * 0.0) != 0.0) { /* nan and inf test */ + return NULL; + } + new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (new_value == NULL) { return NULL; } new_value->parent = NULL; diff --git a/tests.c b/tests.c index 87b0087..aec8780 100644 --- a/tests.c +++ b/tests.c @@ -436,6 +436,10 @@ void test_suite_5(void) { TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4, 5]"))); json_array_remove(remove_test_arr, 2); TEST(json_value_equals(remove_test_val, json_parse_string("[2, 4]"))); + + /* Testing nan and inf */ + TEST(json_object_set_number(obj, "num", 0.0 / 0.0) == JSONFailure); + TEST(json_object_set_number(obj, "num", 1.0 / 0.0) == JSONFailure); } void test_suite_6(void) { @@ -505,28 +509,29 @@ void test_suite_9(void) { TEST((strlen(serialized)+1) == serialization_size); file_contents = read_file(filename); + TEST(STREQ(file_contents, serialized)); } void test_suite_10(void) { - JSON_Value *val1; + JSON_Value *val; char *serialized; malloc_count = 0; - val1 = json_parse_file("tests/test_1_1.txt"); - json_value_free(val1); + val = json_parse_file("tests/test_1_1.txt"); + json_value_free(val); - val1 = json_parse_file("tests/test_1_3.txt"); - json_value_free(val1); + val = json_parse_file("tests/test_1_3.txt"); + json_value_free(val); - val1 = json_parse_file("tests/test_2.txt"); - serialized = json_serialize_to_string_pretty(val1); + val = json_parse_file("tests/test_2.txt"); + serialized = json_serialize_to_string_pretty(val); json_free_serialized_string(serialized); - json_value_free(val1); + json_value_free(val); - val1 = json_parse_file("tests/test_2_pretty.txt"); - json_value_free(val1); + val = json_parse_file("tests/test_2_pretty.txt"); + json_value_free(val); TEST(malloc_count == 0); } diff --git a/tests/test_2.txt b/tests/test_2.txt index 160e7d7..f473599 100644 --- a/tests/test_2.txt +++ b/tests/test_2.txt @@ -7,6 +7,8 @@ "negative one" : -1, "pi" : 3.14, "hard to parse number" : -3.14e-4, + "big int": 2147483647, + "big uint": 4294967295, "boolean true" : true, "boolean false" : false, "null" : null, diff --git a/tests/test_2_comments.txt b/tests/test_2_comments.txt index 5ecf129..504be86 100644 --- a/tests/test_2_comments.txt +++ b/tests/test_2_comments.txt @@ -13,6 +13,8 @@ "negative one" : -1, "pi" : 3.14, "hard to parse number" : -3.14e-4, + "big int": 2147483647, + "big uint": 4294967295, "boolean true" : true, "boolean false" : false, "null" : null, diff --git a/tests/test_2_pretty.txt b/tests/test_2_pretty.txt index fe0adfd..96aaaef 100644 --- a/tests/test_2_pretty.txt +++ b/tests/test_2_pretty.txt @@ -5,8 +5,10 @@ "surrogate string": "loremš¯„˛ipsumš¯¨§lorem", "positive one": 1, "negative one": -1, - "pi": 3.140000, - "hard to parse number": -0.000314, + "pi": 3.1400000000000001, + "hard to parse number": -0.00031399999999999999, + "big int": 2147483647, + "big uint": 4294967295, "boolean true": true, "boolean false": false, "null": null,