]> git.feebdaed.xyz Git - 0xmirror/cJSON.git/commitdiff
feat: add tests for #842
authorAlanscut <wp_scut@163.com>
Thu, 9 May 2024 02:45:16 +0000 (10:45 +0800)
committerAlan Wang <wp_scut@163.com>
Mon, 13 May 2024 09:18:46 +0000 (17:18 +0800)
Add some tests for setting NULL to deallocated pointers
releated to #842 and #833

tests/CMakeLists.txt
tests/misc_tests.c

index c7592213b91236600136e670c7be4d5834a6fbe1..9e8962f61bf3cad7e0206084b391af3cc778d2f5 100644 (file)
@@ -62,6 +62,7 @@ if(ENABLE_CJSON_TEST)
 
     option(ENABLE_VALGRIND OFF "Enable the valgrind memory checker for the tests.")
     if (ENABLE_VALGRIND)
+        add_compile_definitions(ENABLE_VALGRIND)
         find_program(MEMORYCHECK_COMMAND valgrind)
         if ("${MEMORYCHECK_COMMAND}" MATCHES "MEMORYCHECK_COMMAND-NOTFOUND")
             message(WARNING "Valgrind couldn't be found.")
index ba3e003e5d45253f4e07c786a59c899899300410..94dd91aa69f63c67417e650e419735b48b436840 100644 (file)
@@ -732,6 +732,23 @@ static void cjson_set_bool_value_must_not_break_objects(void)
     cJSON_Delete(sobj);
 }
 
+static void deallocated_pointers_should_be_set_to_null(void)
+{
+    /* deallocated pointers should be set to null */
+    /* however, valgrind on linux reports when attempting to access a freed memory, we have to skip it */
+#ifndef ENABLE_VALGRIND
+    cJSON *string = cJSON_CreateString("item");
+    cJSON *root = cJSON_CreateObject();
+
+    cJSON_Delete(string);
+    free(string->valuestring);
+
+    cJSON_AddObjectToObject(root, "object");
+    cJSON_Delete(root->child);
+    free(root->child->string);
+#endif
+}
+
 int CJSON_CDECL main(void)
 {
     UNITY_BEGIN();
@@ -762,6 +779,7 @@ int CJSON_CDECL main(void)
     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);
+    RUN_TEST(deallocated_pointers_should_be_set_to_null);
 
     return UNITY_END();
 }