cJSON
[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)
Brought by drakestapleton
cJSON / tests / misc_utils_tests.c
| 1 | /* |
| 2 | Copyright (c) 2009-2017 Dave Gamble and cJSON contributors |
| 3 | |
| 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | of this software and associated documentation files (the "Software"), to deal |
| 6 | in the Software without restriction, including without limitation the rights |
| 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | copies of the Software, and to permit persons to whom the Software is |
| 9 | furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | The above copyright notice and this permission notice shall be included in |
| 12 | all copies or substantial portions of the Software. |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "unity/examples/unity_config.h" |
| 28 | #include "unity/src/unity.h" |
| 29 | #include "common.h" |
| 30 | #include "../cJSON_Utils.h" |
| 31 | |
| 32 | static void cjson_utils_functions_shouldnt_crash_with_null_pointers(void) |
| 33 | { |
| 34 | cJSON *item = cJSON_CreateString("item"); |
| 35 | TEST_ASSERT_NOT_NULL(item); |
| 36 | |
| 37 | TEST_ASSERT_NULL(cJSONUtils_GetPointer(item, NULL)); |
| 38 | TEST_ASSERT_NULL(cJSONUtils_GetPointer(NULL, "pointer")); |
| 39 | TEST_ASSERT_NULL(cJSONUtils_GetPointerCaseSensitive(NULL, "pointer")); |
| 40 | TEST_ASSERT_NULL(cJSONUtils_GetPointerCaseSensitive(item, NULL)); |
| 41 | TEST_ASSERT_NULL(cJSONUtils_GeneratePatches(item, NULL)); |
| 42 | TEST_ASSERT_NULL(cJSONUtils_GeneratePatches(NULL, item)); |
| 43 | TEST_ASSERT_NULL(cJSONUtils_GeneratePatchesCaseSensitive(item, NULL)); |
| 44 | TEST_ASSERT_NULL(cJSONUtils_GeneratePatchesCaseSensitive(NULL, item)); |
| 45 | cJSONUtils_AddPatchToArray(item, "path", "add", NULL); |
| 46 | cJSONUtils_AddPatchToArray(item, "path", NULL, item); |
| 47 | cJSONUtils_AddPatchToArray(item, NULL, "add", item); |
| 48 | cJSONUtils_AddPatchToArray(NULL, "path", "add", item); |
| 49 | cJSONUtils_ApplyPatches(item, NULL); |
| 50 | cJSONUtils_ApplyPatches(NULL, item); |
| 51 | cJSONUtils_ApplyPatchesCaseSensitive(item, NULL); |
| 52 | cJSONUtils_ApplyPatchesCaseSensitive(NULL, item); |
| 53 | TEST_ASSERT_NULL(cJSONUtils_MergePatch(item, NULL)); |
| 54 | item = cJSON_CreateString("item"); |
| 55 | TEST_ASSERT_NULL(cJSONUtils_MergePatchCaseSensitive(item, NULL)); |
| 56 | item = cJSON_CreateString("item"); |
| 57 | /* these calls are actually valid */ |
| 58 | /* cJSONUtils_MergePatch(NULL, item); */ |
| 59 | /* cJSONUtils_MergePatchCaseSensitive(NULL, item);*/ |
| 60 | /* cJSONUtils_GenerateMergePatch(item, NULL); */ |
| 61 | /* cJSONUtils_GenerateMergePatch(NULL, item); */ |
| 62 | /* cJSONUtils_GenerateMergePatchCaseSensitive(item, NULL); */ |
| 63 | /* cJSONUtils_GenerateMergePatchCaseSensitive(NULL, item); */ |
| 64 | |
| 65 | TEST_ASSERT_NULL(cJSONUtils_FindPointerFromObjectTo(item, NULL)); |
| 66 | TEST_ASSERT_NULL(cJSONUtils_FindPointerFromObjectTo(NULL, item)); |
| 67 | cJSONUtils_SortObject(NULL); |
| 68 | cJSONUtils_SortObjectCaseSensitive(NULL); |
| 69 | |
| 70 | cJSON_Delete(item); |
| 71 | } |
| 72 | |
| 73 | int main(void) |
| 74 | { |
| 75 | UNITY_BEGIN(); |
| 76 | |
| 77 | RUN_TEST(cjson_utils_functions_shouldnt_crash_with_null_pointers); |
| 78 | |
| 79 | return UNITY_END(); |
| 80 | } |