cjson/backport-Add-test-for-heap-buffer-overflow.patch
Zhao Mengmeng de2287f574 Sync patches from 24.03 branch
Sync these patches from 24.03 branch:
- Fix-a-null-pointer-crash-in-cJSON_ReplaceItemViaPoin.patch
- backport-Add-test-for-heap-buffer-overflow.patch
- backport-Fix-heap-buffer-overflow.patch
- backport-fix-add-allocate-check-for-replace_item_in_object-67.patch
- backport-fix-print-int-without-decimal-places-630.patch
- backport-Set-free-d-pointers-to-NULL-whenever-they-are-not-re.patch

Signed-off-by: Zhao Mengmeng <zhaomengmeng@kylinos.cn>
2024-06-18 16:38:13 +08:00

59 lines
1.5 KiB
Diff

From 826cd6f842ae7e46ee38bbc097f9a34f2947388d Mon Sep 17 00:00:00 2001
From: orri <orri@systemb.is>
Date: Tue, 30 Apr 2024 09:46:17 +0000
Subject: [PATCH 1/2] Add test for heap buffer overflow
From #800
---
tests/parse_examples.c | 28 ++++++++++++++++++++++++++++
1 file changed, 28 insertions(+)
diff --git a/tests/parse_examples.c b/tests/parse_examples.c
index 95a0959..d35d6cf 100644
--- a/tests/parse_examples.c
+++ b/tests/parse_examples.c
@@ -250,6 +250,33 @@ static void test14_should_not_be_parsed(void)
}
}
+/* Address Sanitizer */
+static void test15_should_not_heap_buffer_overflow(void)
+{
+ const char *strings[] = {
+ "{\"1\":1,",
+ "{\"1\":1, ",
+ };
+
+ size_t i;
+
+ for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i+=1)
+ {
+ const char *json_string = strings[i];
+ size_t len = strlen(json_string);
+ cJSON *json = NULL;
+
+ char *exact_size_heap = (char*)malloc(len);
+ TEST_ASSERT_NOT_NULL(exact_size_heap);
+
+ memcpy(exact_size_heap, json_string, len);
+ json = cJSON_ParseWithLength(exact_size_heap, len);
+
+ cJSON_Delete(json);
+ free(exact_size_heap);
+ }
+}
+
int CJSON_CDECL main(void)
{
UNITY_BEGIN();
@@ -267,5 +294,6 @@ int CJSON_CDECL main(void)
RUN_TEST(test12_should_not_be_parsed);
RUN_TEST(test13_should_be_parsed_without_null_termination);
RUN_TEST(test14_should_not_be_parsed);
+ RUN_TEST(test15_should_not_heap_buffer_overflow);
return UNITY_END();
}
--
2.43.0