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 / readme_examples.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 | |
| 31 | static const char *json = "{\n\ |
| 32 | \t\"name\":\t\"Awesome 4K\",\n\ |
| 33 | \t\"resolutions\":\t[{\n\ |
| 34 | \t\t\t\"width\":\t1280,\n\ |
| 35 | \t\t\t\"height\":\t720\n\ |
| 36 | \t\t}, {\n\ |
| 37 | \t\t\t\"width\":\t1920,\n\ |
| 38 | \t\t\t\"height\":\t1080\n\ |
| 39 | \t\t}, {\n\ |
| 40 | \t\t\t\"width\":\t3840,\n\ |
| 41 | \t\t\t\"height\":\t2160\n\ |
| 42 | \t\t}]\n\ |
| 43 | }"; |
| 44 | |
| 45 | static char* create_monitor(void) |
| 46 | { |
| 47 | const unsigned int resolution_numbers[3][2] = { |
| 48 | {1280, 720}, |
| 49 | {1920, 1080}, |
| 50 | {3840, 2160} |
| 51 | }; |
| 52 | char *string = NULL; |
| 53 | cJSON *name = NULL; |
| 54 | cJSON *resolutions = NULL; |
| 55 | cJSON *resolution = NULL; |
| 56 | cJSON *width = NULL; |
| 57 | cJSON *height = NULL; |
| 58 | size_t index = 0; |
| 59 | |
| 60 | cJSON *monitor = cJSON_CreateObject(); |
| 61 | if (monitor == NULL) |
| 62 | { |
| 63 | goto end; |
| 64 | } |
| 65 | |
| 66 | name = cJSON_CreateString("Awesome 4K"); |
| 67 | if (name == NULL) |
| 68 | { |
| 69 | goto end; |
| 70 | } |
| 71 | /* after creation was successful, immediately add it to the monitor, |
| 72 | * thereby transferring ownership of the pointer to it */ |
| 73 | cJSON_AddItemToObject(monitor, "name", name); |
| 74 | |
| 75 | resolutions = cJSON_CreateArray(); |
| 76 | if (resolutions == NULL) |
| 77 | { |
| 78 | goto end; |
| 79 | } |
| 80 | cJSON_AddItemToObject(monitor, "resolutions", resolutions); |
| 81 | |
| 82 | for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index) |
| 83 | { |
| 84 | resolution = cJSON_CreateObject(); |
| 85 | if (resolution == NULL) |
| 86 | { |
| 87 | goto end; |
| 88 | } |
| 89 | cJSON_AddItemToArray(resolutions, resolution); |
| 90 | |
| 91 | width = cJSON_CreateNumber(resolution_numbers[index][0]); |
| 92 | if (width == NULL) |
| 93 | { |
| 94 | goto end; |
| 95 | } |
| 96 | cJSON_AddItemToObject(resolution, "width", width); |
| 97 | |
| 98 | height = cJSON_CreateNumber(resolution_numbers[index][1]); |
| 99 | if (height == NULL) |
| 100 | { |
| 101 | goto end; |
| 102 | } |
| 103 | cJSON_AddItemToObject(resolution, "height", height); |
| 104 | } |
| 105 | |
| 106 | string = cJSON_Print(monitor); |
| 107 | if (string == NULL) |
| 108 | { |
| 109 | fprintf(stderr, "Failed to print monitor.\n"); |
| 110 | } |
| 111 | |
| 112 | end: |
| 113 | cJSON_Delete(monitor); |
| 114 | return string; |
| 115 | } |
| 116 | |
| 117 | static char *create_monitor_with_helpers(void) |
| 118 | { |
| 119 | const unsigned int resolution_numbers[3][2] = { |
| 120 | {1280, 720}, |
| 121 | {1920, 1080}, |
| 122 | {3840, 2160} |
| 123 | }; |
| 124 | char *string = NULL; |
| 125 | cJSON *resolutions = NULL; |
| 126 | size_t index = 0; |
| 127 | |
| 128 | cJSON *monitor = cJSON_CreateObject(); |
| 129 | |
| 130 | if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL) |
| 131 | { |
| 132 | goto end; |
| 133 | } |
| 134 | |
| 135 | resolutions = cJSON_AddArrayToObject(monitor, "resolutions"); |
| 136 | if (resolutions == NULL) |
| 137 | { |
| 138 | goto end; |
| 139 | } |
| 140 | |
| 141 | for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index) |
| 142 | { |
| 143 | cJSON *resolution = cJSON_CreateObject(); |
| 144 | |
| 145 | if (cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]) == NULL) |
| 146 | { |
| 147 | goto end; |
| 148 | } |
| 149 | |
| 150 | if(cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]) == NULL) |
| 151 | { |
| 152 | goto end; |
| 153 | } |
| 154 | |
| 155 | cJSON_AddItemToArray(resolutions, resolution); |
| 156 | } |
| 157 | |
| 158 | string = cJSON_Print(monitor); |
| 159 | if (string == NULL) { |
| 160 | fprintf(stderr, "Failed to print monitor.\n"); |
| 161 | } |
| 162 | |
| 163 | end: |
| 164 | cJSON_Delete(monitor); |
| 165 | return string; |
| 166 | } |
| 167 | |
| 168 | /* return 1 if the monitor supports full hd, 0 otherwise */ |
| 169 | static int supports_full_hd(const char * const monitor) |
| 170 | { |
| 171 | const cJSON *resolution = NULL; |
| 172 | const cJSON *resolutions = NULL; |
| 173 | const cJSON *name = NULL; |
| 174 | int status = 0; |
| 175 | cJSON *monitor_json = cJSON_Parse(monitor); |
| 176 | if (monitor_json == NULL) |
| 177 | { |
| 178 | const char *error_ptr = cJSON_GetErrorPtr(); |
| 179 | if (error_ptr != NULL) |
| 180 | { |
| 181 | fprintf(stderr, "Error before: %s\n", error_ptr); |
| 182 | } |
| 183 | status = 0; |
| 184 | goto end; |
| 185 | } |
| 186 | |
| 187 | name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name"); |
| 188 | if (cJSON_IsString(name) && (name->valuestring != NULL)) |
| 189 | { |
| 190 | printf("Checking monitor \"%s\"\n", name->valuestring); |
| 191 | } |
| 192 | |
| 193 | resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions"); |
| 194 | cJSON_ArrayForEach(resolution, resolutions) |
| 195 | { |
| 196 | cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width"); |
| 197 | cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height"); |
| 198 | |
| 199 | if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height)) |
| 200 | { |
| 201 | status = 0; |
| 202 | goto end; |
| 203 | } |
| 204 | |
| 205 | if (compare_double(width->valuedouble, 1920) && compare_double(height->valuedouble, 1080)) |
| 206 | { |
| 207 | status = 1; |
| 208 | goto end; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | end: |
| 213 | cJSON_Delete(monitor_json); |
| 214 | return status; |
| 215 | } |
| 216 | |
| 217 | static void create_monitor_should_create_a_monitor(void) |
| 218 | { |
| 219 | char *monitor = create_monitor(); |
| 220 | |
| 221 | TEST_ASSERT_EQUAL_STRING(monitor, json); |
| 222 | |
| 223 | free(monitor); |
| 224 | } |
| 225 | |
| 226 | static void create_monitor_with_helpers_should_create_a_monitor(void) |
| 227 | { |
| 228 | char *monitor = create_monitor_with_helpers(); |
| 229 | |
| 230 | TEST_ASSERT_EQUAL_STRING(json, monitor); |
| 231 | |
| 232 | free(monitor); |
| 233 | } |
| 234 | |
| 235 | static void supports_full_hd_should_check_for_full_hd_support(void) |
| 236 | { |
| 237 | static const char *monitor_without_hd = "{\n\ |
| 238 | \t\t\"name\": \"lame monitor\",\n\ |
| 239 | \t\t\"resolutions\":\t[{\n\ |
| 240 | \t\t\t\"width\":\t640,\n\ |
| 241 | \t\t\t\"height\":\t480\n\ |
| 242 | \t\t}]\n\ |
| 243 | }"; |
| 244 | |
| 245 | TEST_ASSERT(supports_full_hd(json)); |
| 246 | TEST_ASSERT_FALSE(supports_full_hd(monitor_without_hd)); |
| 247 | } |
| 248 | |
| 249 | int CJSON_CDECL main(void) |
| 250 | { |
| 251 | UNITY_BEGIN(); |
| 252 | |
| 253 | RUN_TEST(create_monitor_should_create_a_monitor); |
| 254 | RUN_TEST(create_monitor_with_helpers_should_create_a_monitor); |
| 255 | RUN_TEST(supports_full_hd_should_check_for_full_hd_support); |
| 256 | |
| 257 | return UNITY_END(); |
| 258 | } |