Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b502cd

Browse files
committed
feat: add tests for DaveGamble#842
Add some tests for setting NULL to deallocated pointers releated to DaveGamble#842 and DaveGamble#833
1 parent 542fb0e commit 5b502cd

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

‎tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ if(ENABLE_CJSON_TEST)
6262

6363
option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")
6464
if (ENABLE_VALGRIND)
65+
add_compile_definitions(ENABLE_VALGRIND)
6566
find_program(MEMORYCHECK_COMMAND valgrind)
6667
if ("${MEMORYCHECK_COMMAND}" MATCHES "MEMORYCHECK_COMMAND-NOTFOUND")
6768
message(WARNING "Valgrind couldn't be found.")

‎tests/misc_tests.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,23 @@ static void cjson_set_bool_value_must_not_break_objects(void)
732732
cJSON_Delete(sobj);
733733
}
734734

735+
static void deallocated_pointers_should_be_set_to_null(void)
736+
{
737+
/* deallocated pointers should be set to null */
738+
/* however, valgrind on linux reports when attempting to access a freed memory, we have to skip it */
739+
#ifndef ENABLE_VALGRIND
740+
cJSON *string = cJSON_CreateString("item");
741+
cJSON *root = cJSON_CreateObject();
742+
743+
cJSON_Delete(string);
744+
free(string->valuestring);
745+
746+
cJSON_AddObjectToObject(root, "object");
747+
cJSON_Delete(root->child);
748+
free(root->child->string);
749+
#endif
750+
}
751+
735752
int CJSON_CDECL main(void)
736753
{
737754
UNITY_BEGIN();
@@ -762,6 +779,7 @@ int CJSON_CDECL main(void)
762779
RUN_TEST(cjson_delete_item_from_array_should_not_broken_list_structure);
763780
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
764781
RUN_TEST(cjson_set_bool_value_must_not_break_objects);
782+
RUN_TEST(deallocated_pointers_should_be_set_to_null);
765783

766784
return UNITY_END();
767785
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /