Fixed realloc(). Supplying a NULL pointer crashed. Furthermore, a size

argument value of 0 should only free the old allocation.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15688 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2005-12-27 21:36:51 +00:00
parent 6470bb6152
commit 9d577c1064
+12 -5
View File
@@ -292,17 +292,24 @@ realloc(void *oldBuffer, size_t newSize)
{
// ToDo: improve this implementation!
if (newSize == 0) {
free(oldBuffer);
return NULL;
}
void *newBuffer = malloc(newSize);
if (newBuffer == NULL)
return NULL;
free_chunk *oldChunk = free_chunk::SetToAllocated(oldBuffer);
if (oldBuffer) {
free_chunk *oldChunk = free_chunk::SetToAllocated(oldBuffer);
if (newSize > oldChunk->size)
newSize = oldChunk->size;
if (newSize > oldChunk->size)
newSize = oldChunk->size;
memcpy(newBuffer, oldBuffer, newSize);
free(oldBuffer);
memcpy(newBuffer, oldBuffer, newSize);
free(oldBuffer);
}
return newBuffer;
}