diff --git a/testsuites/unittest/fuzz/src/stdlib/realloc_fuzz.c b/testsuites/unittest/fuzz/src/stdlib/realloc_fuzz.c index 96a6d97e..eebda93e 100644 --- a/testsuites/unittest/fuzz/src/stdlib/realloc_fuzz.c +++ b/testsuites/unittest/fuzz/src/stdlib/realloc_fuzz.c @@ -46,6 +46,7 @@ extern int g_iteration; void ReallocFuzzTest(void) { char *source = NULL; + char *buf = NULL; int c; printf("Fuzz test in line [%d] realloc start\n", __LINE__); @@ -59,6 +60,7 @@ void ReallocFuzzTest(void) INIT_FuzzEnvironment(); CreatPrecondForQueue(); for (int i = 0; i < CYCLE_TOTAL_TIMES; i++) { + source = buf; hi_watchdog_feed(); heartbeatPrint(i); @@ -66,10 +68,13 @@ void ReallocFuzzTest(void) if ((c <= 0) || (c > TEST_MAX_BUF_LEN)) { c = TEST_BUF_LEN; } - source = (char *)realloc(source, c); + buf = (char *)realloc(source, c); + if (buf == NULL) { + break; + } } - if (source == NULL) { + if (buf == NULL) { free(source); } diff --git a/testsuites/unittest/posix/src/string/memory_func_test.c b/testsuites/unittest/posix/src/string/memory_func_test.c index 6faf65f7..6b553131 100644 --- a/testsuites/unittest/posix/src/string/memory_func_test.c +++ b/testsuites/unittest/posix/src/string/memory_func_test.c @@ -270,19 +270,22 @@ LITE_TEST_CASE(PosixMemFuncTestSuite, testOsMemRealloc001, Function | MediumTest memset(mem, testChar, mlen); rlen = rand() % (1024) + mlen; - mem = realloc(mem, rlen); - TEST_ASSERT_NOT_NULL(mem); + char *mem1 = realloc(mem, rlen); + if (mem1 == NULL) { + free(mem); + } + TEST_ASSERT_NOT_NULL(mem1); len = mlen <= rlen ? mlen : rlen; - data = (char *)mem; + data = (char *)mem1; for (k = 0; k < len; k++) { if (data[k] != testChar) { failure = 1; } } - free(mem); + free(mem1); TEST_ASSERT_EQUAL_INT(failure, 0); } };