Fix overflow by getting the chunk size during reallocation

This commit is contained in:
David Carlier
2018-08-09 12:57:28 +00:00
committed by David Carlier
parent 6a43e654b6
commit 6bac05a057
2 changed files with 6 additions and 2 deletions

View File

@@ -118,6 +118,7 @@ static void *MemOSAlloc(sxu32 nBytes, SyMemBackend *pBackend) {
static void *MemOSRealloc(void *pOld, sxu32 nBytes, SyMemBackend *pBackend) {
sxu32 *pOldChunk;
sxu32 *pChunk;
sxu32 pBytes = nBytes;
CheckHeap(pBackend, nBytes);
pOldChunk = (sxu32 *)(((char *)pOld) - sizeof(sxu32));
if(pOldChunk[0] >= nBytes) {
@@ -128,7 +129,10 @@ static void *MemOSRealloc(void *pOld, sxu32 nBytes, SyMemBackend *pBackend) {
return 0;
}
pChunk[0] = nBytes;
AddToHeap(pBackend, abs(nBytes - pOldChunk[0]));
if(pOldChunk) {
pBytes -= pBackend->pMethods->xChunkSize(pOldChunk);
}
AddToHeap(pBackend, pBytes);
return (void *)&pChunk[1];
}
static void MemOSFree(void *pBlock, SyMemBackend *pBackend) {