Skip to content

Commit b0c42db

Browse files
openharmony_cigitee-org
authored andcommitted
!99 add tdd
Merge pull request !99 from liuyuxiu/master
2 parents 4e9aa24 + 5da3a04 commit b0c42db

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/parse_examples.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,33 @@ static void test14_should_not_be_parsed(void)
250250
}
251251
}
252252

253+
/* Address Sanitizer */
254+
static void test15_should_not_heap_buffer_overflow(void)
255+
{
256+
const char *strings[] = {
257+
"{\"1\":1,",
258+
"{\"1\":1, ",
259+
};
260+
261+
size_t i;
262+
263+
for (i = 0; i < sizeof(strings) / sizeof(strings[0]); i+=1)
264+
{
265+
const char *json_string = strings[i];
266+
size_t len = strlen(json_string);
267+
cJSON *json = NULL;
268+
269+
char *exact_size_heap = (char*)malloc(len);
270+
TEST_ASSERT_NOT_NULL(exact_size_heap);
271+
272+
memcpy(exact_size_heap, json_string, len);
273+
json = cJSON_ParseWithLength(exact_size_heap, len);
274+
275+
cJSON_Delete(json);
276+
free(exact_size_heap);
277+
}
278+
}
279+
253280
int CJSON_CDECL main(void)
254281
{
255282
UNITY_BEGIN();
@@ -267,5 +294,6 @@ int CJSON_CDECL main(void)
267294
RUN_TEST(test12_should_not_be_parsed);
268295
RUN_TEST(test13_should_be_parsed_without_null_termination);
269296
RUN_TEST(test14_should_not_be_parsed);
297+
RUN_TEST(test15_should_not_heap_buffer_overflow);
270298
return UNITY_END();
271299
}

0 commit comments

Comments
 (0)