Skip to content

Commit d3b3304

Browse files
author
Siva Chandra Reddy
committed
[libc] Add a functioning realloc for hermetic tests.
Reviewed By: jhuber6 Differential Revision: https://reviews.llvm.org/D150846
1 parent 625d692 commit d3b3304

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

libc/test/UnitTest/HermeticTestUtils.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,20 @@ void *malloc(size_t s) {
7474

7575
void free(void *) {}
7676

77-
void *realloc(void *ptr, size_t s) {
78-
free(ptr);
79-
return malloc(s);
77+
void *realloc(void *mem, size_t s) {
78+
if (mem == nullptr)
79+
return malloc(s);
80+
uint8_t *newmem = reinterpret_cast<uint8_t *>(malloc(s));
81+
if (newmem == nullptr)
82+
return nullptr;
83+
uint8_t *oldmem = reinterpret_cast<uint8_t *>(mem);
84+
// We use a simple for loop to copy the data over.
85+
// If |s| is less the previous alloc size, the copy works as expected.
86+
// If |s| is greater than the previous alloc size, then garbage is copied
87+
// over to the additional part in the new memory block.
88+
for (size_t i = 0; i < s; ++i)
89+
newmem[i] = oldmem[i];
90+
return newmem;
8091
}
8192

8293
// The unit test framework uses pure virtual functions. Since hermetic tests

libc/test/src/__support/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ add_libc_test(
9595

9696
add_libc_test(
9797
char_vector_test
98-
# This test relies on 'realloc' which is not implemented for hermetic tests.
99-
UNIT_TEST_ONLY
10098
SUITE
10199
libc-support-tests
102100
SRCS

libc/test/src/__support/CPP/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ add_libc_test(
101101

102102
add_libc_test(
103103
string_test
104-
# This test relies on 'realloc' which is not implemented for hermetic tests.
105-
UNIT_TEST_ONLY
106104
SUITE
107105
libc-cpp-utils-tests
108106
SRCS

0 commit comments

Comments
 (0)