From 3c4ee26dbb3df177a2d7b9d80e154ec435ca8c01 Mon Sep 17 00:00:00 2001 From: Krzysztof Gabis Date: Wed, 15 Feb 2023 22:59:21 +0100 Subject: [PATCH] 1.5.1: Fixes a bug in json_object_clear. Issue #200 --- CMakeLists.txt | 2 +- Makefile | 4 ++-- meson.build | 2 +- package.json | 2 +- parson.c | 13 ++++++++--- parson.h | 8 +++---- tests.c | 62 ++++++++++++++++++++++++++++++++++---------------- 7 files changed, 62 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d656fa..4d0d24a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(parson C) include (GNUInstallDirs) -set(PARSON_VERSION 1.5.0) +set(PARSON_VERSION 1.5.1) add_library(parson parson.c) target_include_directories(parson PUBLIC $) diff --git a/Makefile b/Makefile index 0803434..5e6bdfd 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ CC = gcc -CFLAGS = -O0 -g -Wall -Wextra -std=c89 -pedantic-errors -DTESTS_MAIN +CFLAGS = -O0 -g -Wall -Wextra -Wno-deprecated-declarations -std=c89 -pedantic-errors -DTESTS_MAIN CPPC = g++ -CPPFLAGS = -O0 -g -Wall -Wextra -DTESTS_MAIN +CPPFLAGS = -O0 -g -Wall -Wextra -Wno-deprecated-declarations -DTESTS_MAIN all: test testcpp test_hash_collisions diff --git a/meson.build b/meson.build index 96b3826..301f2b2 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,5 @@ project('parson', 'c', - version : '1.5.0', + version : '1.5.1', license : 'MIT', meson_version : '>=0.46.0', default_options : [ diff --git a/package.json b/package.json index 70701b5..b6fc08b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "parson", - "version": "1.5.0", + "version": "1.5.1", "repo": "kgabis/parson", "description": "Small json parser and reader", "keywords": [ "json", "parser" ], diff --git a/parson.c b/parson.c index b09fde8..e024332 100644 --- a/parson.c +++ b/parson.c @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: MIT - Parson 1.5.0 (https://github.com/kgabis/parson) - Copyright (c) 2012 - 2022 Krzysztof Gabis + Parson 1.5.1 (https://github.com/kgabis/parson) + Copyright (c) 2012 - 2023 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 @@ -32,7 +32,7 @@ #define PARSON_IMPL_VERSION_MAJOR 1 #define PARSON_IMPL_VERSION_MINOR 5 -#define PARSON_IMPL_VERSION_PATCH 0 +#define PARSON_IMPL_VERSION_PATCH 1 #if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\ || (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\ @@ -2274,9 +2274,15 @@ JSON_Status json_object_clear(JSON_Object *object) { } for (i = 0; i < json_object_get_count(object); i++) { parson_free(object->names[i]); + object->names[i] = NULL; + json_value_free(object->values[i]); + object->values[i] = NULL; } object->count = 0; + for (i = 0; i < object->cell_capacity; i++) { + object->cells[i] = OBJECT_INVALID_IX; + } return JSONSuccess; } @@ -2445,6 +2451,7 @@ void json_set_escape_slashes(int escape_slashes) { void json_set_float_serialization_format(const char *format) { if (parson_float_format) { parson_free(parson_float_format); + parson_float_format = NULL; } if (!format) { parson_float_format = NULL; diff --git a/parson.h b/parson.h index 993f2d3..1aeaf88 100644 --- a/parson.h +++ b/parson.h @@ -1,8 +1,8 @@ /* SPDX-License-Identifier: MIT - Parson 1.5.0 (https://github.com/kgabis/parson) - Copyright (c) 2012 - 2022 Krzysztof Gabis + Parson 1.5.1 (https://github.com/kgabis/parson) + Copyright (c) 2012 - 2023 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 @@ -36,9 +36,9 @@ extern "C" #define PARSON_VERSION_MAJOR 1 #define PARSON_VERSION_MINOR 5 -#define PARSON_VERSION_PATCH 0 +#define PARSON_VERSION_PATCH 1 -#define PARSON_VERSION_STRING "1.5.0" +#define PARSON_VERSION_STRING "1.5.1" #include /* size_t */ diff --git a/tests.c b/tests.c index 5d3f8a0..d60caa5 100644 --- a/tests.c +++ b/tests.c @@ -2,7 +2,7 @@ SPDX-License-Identifier: MIT Parson (https://github.com/kgabis/parson) - Copyright (c) 2012 - 2022 Krzysztof Gabis + Copyright (c) 2012 - 2023 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 @@ -64,6 +64,7 @@ void test_memory_leaks(void); void test_failing_allocations(void); void test_custom_number_format(void); void test_custom_number_serialization_function(void); +void test_object_clear(void); void print_commits_info(const char *username, const char *repo); void persistence_example(void); @@ -134,6 +135,7 @@ int tests_main(int argc, char *argv[]) { test_failing_allocations(); test_custom_number_format(); test_custom_number_serialization_function(); + test_object_clear(); printf("Tests failed: %d\n", g_tests_failed); printf("Tests passed: %d\n", g_tests_passed); @@ -691,19 +693,24 @@ void test_failing_allocations() { } } - json_set_allocation_functions(malloc, free); + json_set_allocation_functions(counted_malloc, counted_free); printf("OK (tested %d failing allocations)\n", n - 1); g_tests_passed++; } void test_custom_number_format() { - char *serialized = NULL; - JSON_Value *val = json_value_init_number(0.6); - json_set_float_serialization_format("%.1f"); - serialized = json_serialize_to_string(val); - TEST(STREQ(serialized, "0.6")); - json_free_serialized_string(serialized); - json_value_free(val); + g_malloc_count = 0; + { + char *serialized = NULL; + JSON_Value *val = json_value_init_number(0.6); + json_set_float_serialization_format("%.1f"); + serialized = json_serialize_to_string(val); + json_set_float_serialization_format(NULL); + TEST(STREQ(serialized, "0.6")); + json_free_serialized_string(serialized); + json_value_free(val); + } + TEST(g_malloc_count == 0); } static int custom_serialization_func_called = 0; @@ -716,16 +723,33 @@ static int custom_serialization_func(double num, char *buf) { } void test_custom_number_serialization_function() { - /* We just test that custom_serialization_func() gets called, not it's performance */ - char *serialized = NULL; - JSON_Value *val = json_value_init_number(0.6); - json_set_number_serialization_function(custom_serialization_func); - serialized = json_serialize_to_string(val); - TEST(STREQ(serialized, "0.6")); - TEST(custom_serialization_func_called); - json_set_number_serialization_function(NULL); - json_free_serialized_string(serialized); - json_value_free(val); + g_malloc_count = 0; + { + /* We just test that custom_serialization_func() gets called, not it's performance */ + char *serialized = NULL; + JSON_Value *val = json_value_init_number(0.6); + json_set_number_serialization_function(custom_serialization_func); + serialized = json_serialize_to_string(val); + TEST(STREQ(serialized, "0.6")); + TEST(custom_serialization_func_called); + json_set_number_serialization_function(NULL); + json_free_serialized_string(serialized); + json_value_free(val); + } + TEST(g_malloc_count == 0); +} + +void test_object_clear() { + g_malloc_count = 0; + { + JSON_Value *val = json_value_init_object(); + JSON_Object *obj = json_value_get_object(val); + json_object_set_string(obj, "foo", "bar"); + json_object_clear(obj); + TEST(json_object_get_value(obj, "foo") == NULL); + json_value_free(val); + } + TEST(g_malloc_count == 0); } void print_commits_info(const char *username, const char *repo) {