Fix two realloc issues.

realloc(*ptr, 0) now frees the pointer instead of looking at the current
size of the pointer.

if the malloc fails realloc should return NULL;
This commit is contained in:
Mark de Wever 2008-12-05 19:51:59 +00:00
parent 2f76ada0c0
commit 334e1e535b

View file

@ -357,13 +357,17 @@ void* valloc(size_t size)
void* realloc(void* ptr, size_t size)
{
if(IS_OUR_PTR(ptr)) {
const int old_size = get_block_from_chunk(ptr)->header.chunk_size;
if(old_size == 0) {
if(size == 0) {
free(ptr);
return NULL;
}
void* new_memory = malloc(size);
if(new_memory == NULL) {
return NULL;
}
const int old_size = get_block_from_chunk(ptr)->header.chunk_size;
const size_t nbytes = size < old_size ? size : old_size;
memcpy(new_memory, ptr, nbytes);
free(ptr);