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
| 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 | #ifndef CJSON_TESTS_COMMON_H |
| 24 | #define CJSON_TESTS_COMMON_H |
| 25 | |
| 26 | #include "../cJSON.c" |
| 27 | |
| 28 | void reset(cJSON *item); |
| 29 | void reset(cJSON *item) { |
| 30 | if ((item != NULL) && (item->child != NULL)) |
| 31 | { |
| 32 | cJSON_Delete(item->child); |
| 33 | } |
| 34 | if ((item->valuestring != NULL) && !(item->type & cJSON_IsReference)) |
| 35 | { |
| 36 | global_hooks.deallocate(item->valuestring); |
| 37 | } |
| 38 | if ((item->string != NULL) && !(item->type & cJSON_StringIsConst)) |
| 39 | { |
| 40 | global_hooks.deallocate(item->string); |
| 41 | } |
| 42 | |
| 43 | memset(item, 0, sizeof(cJSON)); |
| 44 | } |
| 45 | |
| 46 | char* read_file(const char *filename); |
| 47 | char* read_file(const char *filename) { |
| 48 | FILE *file = NULL; |
| 49 | long length = 0; |
| 50 | char *content = NULL; |
| 51 | size_t read_chars = 0; |
| 52 | |
| 53 | /* open in read binary mode */ |
| 54 | file = fopen(filename, "rb"); |
| 55 | if (file == NULL) |
| 56 | { |
| 57 | goto cleanup; |
| 58 | } |
| 59 | |
| 60 | /* get the length */ |
| 61 | if (fseek(file, 0, SEEK_END) != 0) |
| 62 | { |
| 63 | goto cleanup; |
| 64 | } |
| 65 | length = ftell(file); |
| 66 | if (length < 0) |
| 67 | { |
| 68 | goto cleanup; |
| 69 | } |
| 70 | if (fseek(file, 0, SEEK_SET) != 0) |
| 71 | { |
| 72 | goto cleanup; |
| 73 | } |
| 74 | |
| 75 | /* allocate content buffer */ |
| 76 | content = (char*)malloc((size_t)length + sizeof("")); |
| 77 | if (content == NULL) |
| 78 | { |
| 79 | goto cleanup; |
| 80 | } |
| 81 | |
| 82 | /* read the file into memory */ |
| 83 | read_chars = fread(content, sizeof(char), (size_t)length, file); |
| 84 | if ((long)read_chars != length) |
| 85 | { |
| 86 | free(content); |
| 87 | content = NULL; |
| 88 | goto cleanup; |
| 89 | } |
| 90 | content[read_chars] = '\0'; |
| 91 | |
| 92 | |
| 93 | cleanup: |
| 94 | if (file != NULL) |
| 95 | { |
| 96 | fclose(file); |
| 97 | } |
| 98 | |
| 99 | return content; |
| 100 | } |
| 101 | |
| 102 | /* assertion helper macros */ |
| 103 | #define assert_has_type(item, item_type) TEST_ASSERT_BITS_MESSAGE(0xFF, item_type, item->type, "Item doesn't have expected type.") |
| 104 | #define assert_has_no_reference(item) TEST_ASSERT_BITS_MESSAGE(cJSON_IsReference, 0, item->type, "Item should not have a string as reference.") |
| 105 | #define assert_has_no_const_string(item) TEST_ASSERT_BITS_MESSAGE(cJSON_StringIsConst, 0, item->type, "Item should not have a const string.") |
| 106 | #define assert_has_valuestring(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->valuestring, "Valuestring is NULL.") |
| 107 | #define assert_has_no_valuestring(item) TEST_ASSERT_NULL_MESSAGE(item->valuestring, "Valuestring is not NULL.") |
| 108 | #define assert_has_string(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->string, "String is NULL") |
| 109 | #define assert_has_no_string(item) TEST_ASSERT_NULL_MESSAGE(item->string, "String is not NULL.") |
| 110 | #define assert_not_in_list(item) \ |
| 111 | TEST_ASSERT_NULL_MESSAGE(item->next, "Linked list next pointer is not NULL.");\ |
| 112 | TEST_ASSERT_NULL_MESSAGE(item->prev, "Linked list previous pointer is not NULL.") |
| 113 | #define assert_has_child(item) TEST_ASSERT_NOT_NULL_MESSAGE(item->child, "Item doesn't have a child.") |
| 114 | #define assert_has_no_child(item) TEST_ASSERT_NULL_MESSAGE(item->child, "Item has a child.") |
| 115 | #define assert_is_invalid(item) \ |
| 116 | assert_has_type(item, cJSON_Invalid);\ |
| 117 | assert_not_in_list(item);\ |
| 118 | assert_has_no_child(item);\ |
| 119 | assert_has_no_string(item);\ |
| 120 | assert_has_no_valuestring(item) |
| 121 | |
| 122 | #endif |