mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-19 23:10:02 +00:00
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:
parent
2f76ada0c0
commit
334e1e535b
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user