Make _recalloc adhere to MS's definition

This commit is contained in:
HolyWu 2018-04-30 15:41:50 +08:00 committed by Holy Wu
parent fe87ffb7ea
commit f47a52ce85
2 changed files with 20 additions and 5 deletions

View File

@ -1391,7 +1391,7 @@ ATTRIBUTE_ALWAYS_INLINE inline void* do_calloc(size_t n, size_t elem_size) {
void* result = do_malloc_or_cpp_alloc(size); void* result = do_malloc_or_cpp_alloc(size);
if (result != NULL) { if (result != NULL) {
memset(result, 0, size); memset(result, 0, tc_nallocx(size, 0));
} }
return result; return result;
} }

View File

@ -66,10 +66,25 @@ void* _calloc_base(size_t n, size_t size) {
return calloc(n, size); return calloc(n, size);
} }
void* _recalloc(void* p, size_t n, size_t size) { void* _recalloc(void* old_ptr, size_t n, size_t size) {
void* result = realloc(p, n * size); // Ensure that (n * size) does not overflow
memset(result, 0, n * size); if (!(n == 0 || (std::numeric_limits<size_t>::max)() / n >= size)) {
return result; errno = ENOMEM;
return NULL;
}
const size_t old_size = tc_malloc_size(old_ptr);
const size_t new_size = n * size;
void* new_ptr = realloc(old_ptr, new_size);
// If the reallocation succeeded and the new block is larger, zero-fill the
// new bytes:
if (new_ptr != NULL && new_size > old_size) {
memset(static_cast<char*>(new_ptr) + old_size, 0, tc_nallocx(new_size, 0) - old_size);
}
return new_ptr;
} }
void* _calloc_impl(size_t n, size_t size) { void* _calloc_impl(size_t n, size_t size) {