mirror of
https://github.com/kgabis/parson.git
synced 2025-01-28 06:32:55 +08:00
Added additional null checks and tests.
This commit is contained in:
parent
81c2fd0186
commit
0d5ac45286
41
parson.c
41
parson.c
@ -246,7 +246,10 @@ static JSON_Object * json_object_init(void) {
|
||||
}
|
||||
|
||||
static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) {
|
||||
size_t index;
|
||||
size_t index = 0;
|
||||
if (object == NULL || name == NULL || value == NULL) {
|
||||
return JSONFailure;
|
||||
}
|
||||
if (object->count >= object->capacity) {
|
||||
size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY);
|
||||
if (new_capacity > OBJECT_MAX_CAPACITY)
|
||||
@ -258,7 +261,7 @@ static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_V
|
||||
return JSONFailure;
|
||||
index = object->count;
|
||||
object->names[index] = parson_strdup(name);
|
||||
if (!object->names[index])
|
||||
if (object->names[index] == NULL)
|
||||
return JSONFailure;
|
||||
object->values[index] = value;
|
||||
object->count++;
|
||||
@ -677,6 +680,8 @@ char* json_serialize_to_buffer_r(const JSON_Value *value, char *buf)
|
||||
for (i = 0; i < count; i++) {
|
||||
temp_value = json_array_get_value(array, i);
|
||||
buf = json_serialize_to_buffer_r(temp_value, buf);
|
||||
if (buf == NULL)
|
||||
return NULL;
|
||||
if (i < (count - 1))
|
||||
PRINT_AND_SKIP(buf, ",");
|
||||
}
|
||||
@ -689,9 +694,13 @@ char* json_serialize_to_buffer_r(const JSON_Value *value, char *buf)
|
||||
for (i = 0; i < count; i++) {
|
||||
key = json_object_get_name(object, i);
|
||||
buf = json_serialize_string(key, buf);
|
||||
if (buf == NULL)
|
||||
return NULL;
|
||||
PRINT_AND_SKIP(buf, ":");
|
||||
temp_value = json_object_get_value(object, key);
|
||||
buf = json_serialize_to_buffer_r(temp_value, buf);
|
||||
if (buf == NULL)
|
||||
return NULL;
|
||||
if (i < (count - 1))
|
||||
PRINT_AND_SKIP(buf, ",");
|
||||
}
|
||||
@ -751,7 +760,7 @@ static char * json_serialize_string(const char *string, char *buf) {
|
||||
JSON_Value * json_parse_file(const char *filename) {
|
||||
char *file_contents = read_file(filename);
|
||||
JSON_Value *output_value = NULL;
|
||||
if (!file_contents)
|
||||
if (file_contents == NULL)
|
||||
return NULL;
|
||||
output_value = json_parse_string(file_contents);
|
||||
PARSON_FREE(file_contents);
|
||||
@ -761,7 +770,7 @@ JSON_Value * json_parse_file(const char *filename) {
|
||||
JSON_Value * json_parse_file_with_comments(const char *filename) {
|
||||
char *file_contents = read_file(filename);
|
||||
JSON_Value *output_value = NULL;
|
||||
if (!file_contents)
|
||||
if (file_contents == NULL)
|
||||
return NULL;
|
||||
output_value = json_parse_string_with_comments(file_contents);
|
||||
PARSON_FREE(file_contents);
|
||||
@ -769,7 +778,7 @@ JSON_Value * json_parse_file_with_comments(const char *filename) {
|
||||
}
|
||||
|
||||
JSON_Value * json_parse_string(const char *string) {
|
||||
if (!string)
|
||||
if (string == NULL)
|
||||
return NULL;
|
||||
SKIP_WHITESPACES(&string);
|
||||
if (*string != '{' && *string != '[')
|
||||
@ -781,7 +790,7 @@ JSON_Value * json_parse_string_with_comments(const char *string) {
|
||||
JSON_Value *result = NULL;
|
||||
char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL;
|
||||
string_mutable_copy = parson_strdup(string);
|
||||
if (!string_mutable_copy)
|
||||
if (string_mutable_copy == NULL)
|
||||
return NULL;
|
||||
remove_comments(string_mutable_copy, "/*", "*/");
|
||||
remove_comments(string_mutable_copy, "//", "\n");
|
||||
@ -800,6 +809,8 @@ JSON_Value * json_parse_string_with_comments(const char *string) {
|
||||
/* JSON Object API */
|
||||
|
||||
JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) {
|
||||
if (object == NULL || name == NULL)
|
||||
return NULL;
|
||||
return json_object_nget_value(object, name, strlen(name));
|
||||
}
|
||||
|
||||
@ -961,7 +972,10 @@ JSON_Value * json_value_init_array(void) {
|
||||
}
|
||||
|
||||
JSON_Value * json_value_init_string(const char *string) {
|
||||
char *processed_copy = process_string(string, strlen(string));
|
||||
char *processed_copy = NULL;
|
||||
if (string == NULL)
|
||||
return NULL;
|
||||
processed_copy = process_string(string, strlen(string));
|
||||
if (processed_copy == NULL)
|
||||
return NULL;
|
||||
return json_value_init_string_no_copy(processed_copy);
|
||||
@ -1190,7 +1204,7 @@ JSON_Status json_array_append_null(JSON_Array *array) {
|
||||
JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) {
|
||||
size_t i = 0;
|
||||
JSON_Value *old_value;
|
||||
if (object == NULL)
|
||||
if (object == NULL || name == NULL || value == NULL)
|
||||
return JSONFailure;
|
||||
old_value = json_object_get_value(object, name);
|
||||
if (old_value != NULL) { /* free and overwrite old value */
|
||||
@ -1202,8 +1216,8 @@ JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Va
|
||||
}
|
||||
}
|
||||
}
|
||||
json_object_add(object, name, value); /* add new key value pair */
|
||||
return JSONSuccess;
|
||||
/* add new key value pair */
|
||||
return json_object_add(object, name, value);
|
||||
}
|
||||
|
||||
JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) {
|
||||
@ -1223,13 +1237,14 @@ JSON_Status json_object_set_null(JSON_Object *object, const char *name) {
|
||||
}
|
||||
|
||||
JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) {
|
||||
const char *dot_pos = strchr(name, '.');
|
||||
const char *dot_pos = NULL;
|
||||
const char *current_name = NULL;
|
||||
JSON_Object *temp_obj = NULL;
|
||||
JSON_Value *new_value = NULL;
|
||||
if (value == NULL) {
|
||||
if (value == NULL || name == NULL || value == NULL)
|
||||
return JSONFailure;
|
||||
} else if (dot_pos == NULL) {
|
||||
dot_pos = strchr(name, '.');
|
||||
if (dot_pos == NULL) {
|
||||
return json_object_set_value(object, name, value);
|
||||
} else {
|
||||
current_name = parson_strndup(name, dot_pos - name);
|
||||
|
43
tests.c
43
tests.c
@ -238,16 +238,28 @@ void test_suite_4() {
|
||||
void test_suite_5(void) {
|
||||
JSON_Value *val_from_file = json_parse_file("tests/test_5.txt");
|
||||
|
||||
JSON_Value *val = json_value_init_object();
|
||||
JSON_Object *obj = json_value_get_object(val);
|
||||
JSON_Value *val = NULL;
|
||||
JSON_Object *obj = NULL;
|
||||
JSON_Array *interests_arr = NULL;
|
||||
|
||||
val = json_value_init_object();
|
||||
TEST(val != NULL);
|
||||
|
||||
obj = json_value_get_object(val);
|
||||
TEST(obj != NULL);
|
||||
|
||||
TEST(json_object_set_string(obj, "first", "John") == JSONSuccess);
|
||||
TEST(json_object_set_string(obj, "last", "Doe") == JSONSuccess);
|
||||
TEST(json_object_set_number(obj, "age", 25) == JSONSuccess);
|
||||
TEST(json_object_set_boolean(obj, "registered", 1) == JSONSuccess);
|
||||
|
||||
TEST(json_object_set_value(obj, "interests", json_value_init_array()) == JSONSuccess);
|
||||
TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Writing") == JSONSuccess);
|
||||
TEST(json_array_append_string(json_object_get_array(obj, "interests"), "Mountain Biking") == JSONSuccess);
|
||||
TEST(json_array_replace_string(json_object_get_array(obj, "interests"), 0, "Reading") == JSONSuccess);
|
||||
interests_arr = json_object_get_array(obj, "interests");
|
||||
TEST(interests_arr != NULL);
|
||||
TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess);
|
||||
TEST(json_array_append_string(interests_arr, "Mountain Biking") == JSONSuccess);
|
||||
TEST(json_array_replace_string(interests_arr, 0, "Reading") == JSONSuccess);
|
||||
|
||||
TEST(json_object_dotset_string(obj, "favorites.color", "blue") == JSONSuccess);
|
||||
TEST(json_object_dotset_string(obj, "favorites.sport", "running") == JSONSuccess);
|
||||
TEST(json_object_dotset_string(obj, "favorites.fruit", "apple") == JSONSuccess);
|
||||
@ -256,6 +268,27 @@ void test_suite_5(void) {
|
||||
TEST(json_object_set_string(obj, "utf-8 string", "あいうえお") == JSONSuccess);
|
||||
TEST(json_object_set_string(obj, "surrogate string", "lorem\\uD834\\uDD1Eipsum\\uD834\\uDF67lorem") == JSONSuccess);
|
||||
TEST(json_value_equals(val_from_file, val));
|
||||
|
||||
TEST(json_object_set_string(obj, NULL, "") == JSONFailure);
|
||||
TEST(json_object_set_string(obj, "last", NULL) == JSONFailure);
|
||||
TEST(json_object_set_string(obj, NULL, NULL) == JSONFailure);
|
||||
TEST(json_object_set_value(obj, NULL, NULL) == JSONFailure);
|
||||
|
||||
TEST(json_object_dotset_string(obj, NULL, "") == JSONFailure);
|
||||
TEST(json_object_dotset_string(obj, "favorites.color", NULL) == JSONFailure);
|
||||
TEST(json_object_dotset_string(obj, NULL, NULL) == JSONFailure);
|
||||
TEST(json_object_dotset_value(obj, NULL, NULL) == JSONFailure);
|
||||
|
||||
TEST(json_array_append_string(NULL, "lorem") == JSONFailure);
|
||||
TEST(json_array_append_value(interests_arr, NULL) == JSONFailure);
|
||||
TEST(json_array_append_value(NULL, NULL) == JSONFailure);
|
||||
|
||||
TEST(json_array_remove(NULL, 0) == JSONFailure);
|
||||
TEST(json_array_replace_value(interests_arr, 0, NULL) == JSONFailure);
|
||||
TEST(json_array_replace_string(NULL, 0, "lorem") == JSONFailure);
|
||||
TEST(json_array_replace_string(interests_arr, 100, "not existing") == JSONFailure);
|
||||
|
||||
TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure);
|
||||
}
|
||||
|
||||
void test_suite_6(void) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user