1.5.1: Fixes a bug in json_object_clear.

Issue #200
This commit is contained in:
Krzysztof Gabis 2023-02-15 22:59:21 +01:00
parent 1314bf8ad6
commit 3c4ee26dbb
7 changed files with 62 additions and 31 deletions

View File

@ -3,7 +3,7 @@ project(parson C)
include (GNUInstallDirs) include (GNUInstallDirs)
set(PARSON_VERSION 1.5.0) set(PARSON_VERSION 1.5.1)
add_library(parson parson.c) add_library(parson parson.c)
target_include_directories(parson PUBLIC $<INSTALL_INTERFACE:include>) target_include_directories(parson PUBLIC $<INSTALL_INTERFACE:include>)

View File

@ -1,8 +1,8 @@
CC = gcc 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++ 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 all: test testcpp test_hash_collisions

View File

@ -1,5 +1,5 @@
project('parson', 'c', project('parson', 'c',
version : '1.5.0', version : '1.5.1',
license : 'MIT', license : 'MIT',
meson_version : '>=0.46.0', meson_version : '>=0.46.0',
default_options : [ default_options : [

View File

@ -1,6 +1,6 @@
{ {
"name": "parson", "name": "parson",
"version": "1.5.0", "version": "1.5.1",
"repo": "kgabis/parson", "repo": "kgabis/parson",
"description": "Small json parser and reader", "description": "Small json parser and reader",
"keywords": [ "json", "parser" ], "keywords": [ "json", "parser" ],

View File

@ -1,8 +1,8 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Parson 1.5.0 (https://github.com/kgabis/parson) Parson 1.5.1 (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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal 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_MAJOR 1
#define PARSON_IMPL_VERSION_MINOR 5 #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)\ #if (PARSON_VERSION_MAJOR != PARSON_IMPL_VERSION_MAJOR)\
|| (PARSON_VERSION_MINOR != PARSON_IMPL_VERSION_MINOR)\ || (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++) { for (i = 0; i < json_object_get_count(object); i++) {
parson_free(object->names[i]); parson_free(object->names[i]);
object->names[i] = NULL;
json_value_free(object->values[i]); json_value_free(object->values[i]);
object->values[i] = NULL;
} }
object->count = 0; object->count = 0;
for (i = 0; i < object->cell_capacity; i++) {
object->cells[i] = OBJECT_INVALID_IX;
}
return JSONSuccess; return JSONSuccess;
} }
@ -2445,6 +2451,7 @@ void json_set_escape_slashes(int escape_slashes) {
void json_set_float_serialization_format(const char *format) { void json_set_float_serialization_format(const char *format) {
if (parson_float_format) { if (parson_float_format) {
parson_free(parson_float_format); parson_free(parson_float_format);
parson_float_format = NULL;
} }
if (!format) { if (!format) {
parson_float_format = NULL; parson_float_format = NULL;

View File

@ -1,8 +1,8 @@
/* /*
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Parson 1.5.0 (https://github.com/kgabis/parson) Parson 1.5.1 (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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal 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_MAJOR 1
#define PARSON_VERSION_MINOR 5 #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 <stddef.h> /* size_t */ #include <stddef.h> /* size_t */

62
tests.c
View File

@ -2,7 +2,7 @@
SPDX-License-Identifier: MIT SPDX-License-Identifier: MIT
Parson (https://github.com/kgabis/parson) 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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal 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_failing_allocations(void);
void test_custom_number_format(void); void test_custom_number_format(void);
void test_custom_number_serialization_function(void); void test_custom_number_serialization_function(void);
void test_object_clear(void);
void print_commits_info(const char *username, const char *repo); void print_commits_info(const char *username, const char *repo);
void persistence_example(void); void persistence_example(void);
@ -134,6 +135,7 @@ int tests_main(int argc, char *argv[]) {
test_failing_allocations(); test_failing_allocations();
test_custom_number_format(); test_custom_number_format();
test_custom_number_serialization_function(); test_custom_number_serialization_function();
test_object_clear();
printf("Tests failed: %d\n", g_tests_failed); printf("Tests failed: %d\n", g_tests_failed);
printf("Tests passed: %d\n", g_tests_passed); 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); printf("OK (tested %d failing allocations)\n", n - 1);
g_tests_passed++; g_tests_passed++;
} }
void test_custom_number_format() { void test_custom_number_format() {
char *serialized = NULL; g_malloc_count = 0;
JSON_Value *val = json_value_init_number(0.6); {
json_set_float_serialization_format("%.1f"); char *serialized = NULL;
serialized = json_serialize_to_string(val); JSON_Value *val = json_value_init_number(0.6);
TEST(STREQ(serialized, "0.6")); json_set_float_serialization_format("%.1f");
json_free_serialized_string(serialized); serialized = json_serialize_to_string(val);
json_value_free(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; 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() { void test_custom_number_serialization_function() {
/* We just test that custom_serialization_func() gets called, not it's performance */ g_malloc_count = 0;
char *serialized = NULL; {
JSON_Value *val = json_value_init_number(0.6); /* We just test that custom_serialization_func() gets called, not it's performance */
json_set_number_serialization_function(custom_serialization_func); char *serialized = NULL;
serialized = json_serialize_to_string(val); JSON_Value *val = json_value_init_number(0.6);
TEST(STREQ(serialized, "0.6")); json_set_number_serialization_function(custom_serialization_func);
TEST(custom_serialization_func_called); serialized = json_serialize_to_string(val);
json_set_number_serialization_function(NULL); TEST(STREQ(serialized, "0.6"));
json_free_serialized_string(serialized); TEST(custom_serialization_func_called);
json_value_free(val); 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) { void print_commits_info(const char *username, const char *repo) {