Added check on oldsize and newsize in reallocate() and fixed bug with free case

This commit is contained in:
nocturn9x 2021-05-05 22:17:33 +02:00
parent 3b0786b248
commit b8deff6202
1 changed files with 2 additions and 1 deletions

View File

@ -22,10 +22,11 @@ void* reallocate(uint_t oldsize, uint_t newsize, void* ptr) {
/* Allocates/reallocates/deallocates a block
of memory using malloc, realloc and free */
if (oldsize < 0 || newsize < 0) return NULL;
if (oldsize == 0 && ptr == NULL) { // Allocate from 0 to newsize
return malloc(newsize);
}
else if (oldsize == 0 && ptr != NULL) { // Free object (shrink to 0)
else if (oldsize > 0 && ptr != NULL) { // Free object (shrink to 0)
free(ptr);
return NULL;
}