From c9b920c4a3eaef8145584a5cad60d3b52495ee8d Mon Sep 17 00:00:00 2001 From: Krzysztof Gabis Date: Wed, 13 Jan 2016 20:47:47 +0000 Subject: [PATCH] Fixes a bug in json_array_remove and adds relevant tests (thanks to KB for finding this). Also - it's 2016, time to update copyright notices, yay! --- parson.c | 13 +++++++++---- parson.h | 2 +- tests.c | 9 ++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/parson.c b/parson.c index 9ee29df..d807d32 100644 --- a/parson.c +++ b/parson.c @@ -1,6 +1,6 @@ /* Parson ( http://kgabis.github.com/parson/ ) - Copyright (c) 2012 - 2015 Krzysztof Gabis + Copyright (c) 2012 - 2016 Krzysztof Gabis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -1322,21 +1322,26 @@ char * json_serialize_to_string_pretty(const JSON_Value *value) { return buf; } - void json_free_serialized_string(char *string) { parson_free(string); } JSON_Status json_array_remove(JSON_Array *array, size_t ix) { + JSON_Value *temp_value = NULL; size_t last_element_ix = 0; if (array == NULL || ix >= json_array_get_count(array)) { return JSONFailure; } last_element_ix = json_array_get_count(array) - 1; json_value_free(json_array_get_value(array, ix)); + if (ix != last_element_ix) { /* Replace value with one from the end of array */ + temp_value = json_array_get_value(array, last_element_ix); + if (temp_value == NULL) { + return JSONFailure; + } + array->items[ix] = temp_value; + } array->count -= 1; - if (ix != last_element_ix) /* Replace value with one from the end of array */ - array->items[ix] = json_array_get_value(array, last_element_ix); return JSONSuccess; } diff --git a/parson.h b/parson.h index 3c04edf..82168c8 100644 --- a/parson.h +++ b/parson.h @@ -1,6 +1,6 @@ /* Parson ( http://kgabis.github.com/parson/ ) - Copyright (c) 2012 - 2015 Krzysztof Gabis + Copyright (c) 2012 - 2016 Krzysztof Gabis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/tests.c b/tests.c index 9baf8ca..5f04326 100644 --- a/tests.c +++ b/tests.c @@ -1,6 +1,6 @@ /* Parson ( http://kgabis.github.com/parson/ ) - Copyright (c) 2012 - 2015 Krzysztof Gabis + Copyright (c) 2012 - 2016 Krzysztof Gabis Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -311,7 +311,14 @@ void test_suite_5(void) { TEST(json_array_append_string(json_object_get_array(obj, "interests"), NULL) == JSONFailure); + TEST(json_array_append_string(interests_arr, "Writing") == JSONSuccess); + TEST(json_array_remove(interests_arr, 0) == JSONSuccess); + TEST(json_array_remove(interests_arr, 1) == JSONSuccess); + TEST(json_array_remove(interests_arr, 0) == JSONSuccess); + TEST(json_array_remove(interests_arr, 0) == JSONFailure); /* should be empty by now */ + TEST(json_object_remove(obj, "interests") == JSONSuccess); + /* UTF-8 tests */ TEST(json_object_set_string(obj, "correct string", "κόσμε") == JSONSuccess);