File tree 3 files changed +14
-7
lines changed
3 files changed +14
-7
lines changed Original file line number Diff line number Diff line change @@ -74,9 +74,20 @@ void *malloc(size_t s) {
74
74
75
75
void free (void *) {}
76
76
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;
80
91
}
81
92
82
93
// The unit test framework uses pure virtual functions. Since hermetic tests
Original file line number Diff line number Diff line change @@ -95,8 +95,6 @@ add_libc_test(
95
95
96
96
add_libc_test(
97
97
char_vector_test
98
- # This test relies on 'realloc' which is not implemented for hermetic tests.
99
- UNIT_TEST_ONLY
100
98
SUITE
101
99
libc-support-tests
102
100
SRCS
Original file line number Diff line number Diff line change @@ -101,8 +101,6 @@ add_libc_test(
101
101
102
102
add_libc_test(
103
103
string_test
104
- # This test relies on 'realloc' which is not implemented for hermetic tests.
105
- UNIT_TEST_ONLY
106
104
SUITE
107
105
libc-cpp-utils-tests
108
106
SRCS
You can’t perform that action at this time.
0 commit comments