From ef55a19de1a66946d31d09ace46753dfe6942910 Mon Sep 17 00:00:00 2001 From: Stoian Ivanov Date: Wed, 5 Jan 2022 00:36:09 +0200 Subject: [PATCH] cJSON_SetBoolValue plus test --- cJSON.h | 7 ++++++ tests/misc_tests.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/cJSON.h b/cJSON.h index 92907a2..5f61633 100644 --- a/cJSON.h +++ b/cJSON.h @@ -279,6 +279,13 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); /* Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String */ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring); +/* If the object is not a boolean type this does nothing and returns cJSON_Invalid else it returns the new type*/ +#define cJSON_SetBoolValue(object, boolValue) ( \ + (object != NULL && ((object)->type & (cJSON_False|cJSON_True))) ? \ + (object)->type=((object)->type &(~(cJSON_False|cJSON_True)))|((boolValue)?cJSON_True:cJSON_False) : \ + 0\ +) + /* Macro for iterating over an array or object */ #define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next) diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 3bf0a1c..57f8f47 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -650,6 +650,63 @@ static void cjson_set_valuestring_to_object_should_not_leak_memory(void) cJSON_Delete(root); } +static void cjson_set_bool_value_must_not_break_objects(void) +{ + cJSON *bobj, *sobj,*oobj,*refobj=NULL; + + TEST_ASSERT_TRUE((cJSON_SetBoolValue(refobj,1)==cJSON_Invalid)); + + bobj=cJSON_CreateFalse(); + TEST_ASSERT_TRUE(cJSON_IsFalse(bobj)); + TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj,1)==cJSON_True)); + TEST_ASSERT_TRUE(cJSON_IsTrue(bobj)); + cJSON_SetBoolValue(bobj,1); + TEST_ASSERT_TRUE(cJSON_IsTrue(bobj)); + TEST_ASSERT_TRUE((cJSON_SetBoolValue(bobj,0)==cJSON_False)); + TEST_ASSERT_TRUE(cJSON_IsFalse(bobj)); + cJSON_SetBoolValue(bobj,0); + TEST_ASSERT_TRUE(cJSON_IsFalse(bobj)); + + sobj=cJSON_CreateString("test"); + TEST_ASSERT_TRUE(cJSON_IsString(sobj)); + cJSON_SetBoolValue(sobj,1); + TEST_ASSERT_TRUE(cJSON_IsString(sobj)); + cJSON_SetBoolValue(sobj,0); + TEST_ASSERT_TRUE(cJSON_IsString(sobj)); + + oobj=cJSON_CreateObject(); + TEST_ASSERT_TRUE(cJSON_IsObject(oobj)); + cJSON_SetBoolValue(oobj,1); + TEST_ASSERT_TRUE(cJSON_IsObject(oobj)); + cJSON_SetBoolValue(oobj,0); + TEST_ASSERT_TRUE(cJSON_IsObject(oobj)); + + refobj=cJSON_CreateStringReference("conststring"); + TEST_ASSERT_TRUE(cJSON_IsString(refobj)); + TEST_ASSERT_TRUE(refobj->type&cJSON_IsReference); + cJSON_SetBoolValue(refobj,1); + TEST_ASSERT_TRUE(cJSON_IsString(refobj)); + TEST_ASSERT_TRUE(refobj->type&cJSON_IsReference); + cJSON_SetBoolValue(refobj,0); + TEST_ASSERT_TRUE(cJSON_IsString(refobj)); + TEST_ASSERT_TRUE(refobj->type&cJSON_IsReference); + cJSON_Delete(refobj); + + refobj=cJSON_CreateObjectReference(oobj); + TEST_ASSERT_TRUE(cJSON_IsObject(refobj)); + TEST_ASSERT_TRUE(refobj->type&cJSON_IsReference); + cJSON_SetBoolValue(refobj,1); + TEST_ASSERT_TRUE(cJSON_IsObject(refobj)); + TEST_ASSERT_TRUE(refobj->type&cJSON_IsReference); + cJSON_SetBoolValue(refobj,0); + TEST_ASSERT_TRUE(cJSON_IsObject(refobj)); + TEST_ASSERT_TRUE(refobj->type&cJSON_IsReference); + cJSON_Delete(refobj); + + cJSON_Delete(bobj); + cJSON_Delete(sobj); +} + int CJSON_CDECL main(void) { UNITY_BEGIN(); @@ -679,6 +736,7 @@ int CJSON_CDECL main(void) RUN_TEST(cjson_add_item_to_object_should_not_use_after_free_when_string_is_aliased); RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure); RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory); + RUN_TEST(cjson_set_bool_value_must_not_break_objects); return UNITY_END(); }