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 | #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 cJSON item[1]; |
| 32 | |
| 33 | static void assert_is_value(cJSON *value_item, int type) |
| 34 | { |
| 35 | TEST_ASSERT_NOT_NULL_MESSAGE(value_item, "Item is NULL."); |
| 36 | |
| 37 | assert_not_in_list(value_item); |
| 38 | assert_has_type(value_item, type); |
| 39 | assert_has_no_reference(value_item); |
| 40 | assert_has_no_const_string(value_item); |
| 41 | assert_has_no_string(value_item); |
| 42 | } |
| 43 | |
| 44 | static void assert_parse_value(const char *string, int type) |
| 45 | { |
| 46 | parse_buffer buffer = { 0, 0, 0, 0, { 0, 0, 0 } }; |
| 47 | buffer.content = (const unsigned char*) string; |
| 48 | buffer.length = strlen(string) + sizeof(""); |
| 49 | buffer.hooks = global_hooks; |
| 50 | |
| 51 | TEST_ASSERT_TRUE(parse_value(item, &buffer)); |
| 52 | assert_is_value(item, type); |
| 53 | } |
| 54 | |
| 55 | static void parse_value_should_parse_null(void) |
| 56 | { |
| 57 | assert_parse_value("null", cJSON_NULL); |
| 58 | reset(item); |
| 59 | } |
| 60 | |
| 61 | static void parse_value_should_parse_true(void) |
| 62 | { |
| 63 | assert_parse_value("true", cJSON_True); |
| 64 | reset(item); |
| 65 | } |
| 66 | |
| 67 | static void parse_value_should_parse_false(void) |
| 68 | { |
| 69 | assert_parse_value("false", cJSON_False); |
| 70 | reset(item); |
| 71 | } |
| 72 | |
| 73 | static void parse_value_should_parse_number(void) |
| 74 | { |
| 75 | assert_parse_value("1.5", cJSON_Number); |
| 76 | reset(item); |
| 77 | } |
| 78 | |
| 79 | static void parse_value_should_parse_string(void) |
| 80 | { |
| 81 | assert_parse_value("\"\"", cJSON_String); |
| 82 | reset(item); |
| 83 | assert_parse_value("\"hello\"", cJSON_String); |
| 84 | reset(item); |
| 85 | } |
| 86 | |
| 87 | static void parse_value_should_parse_array(void) |
| 88 | { |
| 89 | assert_parse_value("[]", cJSON_Array); |
| 90 | reset(item); |
| 91 | } |
| 92 | |
| 93 | static void parse_value_should_parse_object(void) |
| 94 | { |
| 95 | assert_parse_value("{}", cJSON_Object); |
| 96 | reset(item); |
| 97 | } |
| 98 | |
| 99 | int CJSON_CDECL main(void) |
| 100 | { |
| 101 | /* initialize cJSON item */ |
| 102 | memset(item, 0, sizeof(cJSON)); |
| 103 | UNITY_BEGIN(); |
| 104 | RUN_TEST(parse_value_should_parse_null); |
| 105 | RUN_TEST(parse_value_should_parse_true); |
| 106 | RUN_TEST(parse_value_should_parse_false); |
| 107 | RUN_TEST(parse_value_should_parse_number); |
| 108 | RUN_TEST(parse_value_should_parse_string); |
| 109 | RUN_TEST(parse_value_should_parse_array); |
| 110 | RUN_TEST(parse_value_should_parse_object); |
| 111 | return UNITY_END(); |
| 112 | } |