From 6bac05a057252528b7a4dc3a6513189ef2dde876 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 9 Aug 2018 12:57:28 +0000 Subject: [PATCH] Fix overflow by getting the chunk size during reallocation --- Makefile | 2 +- engine/lib/memory.c | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 0385608..b741166 100644 --- a/Makefile +++ b/Makefile @@ -151,7 +151,7 @@ $(SAPI): $(eval SAPI_OBJS := $(addprefix $(BUILD_DIR)/,$(SAPI_MAKE))) $(eval SAPI_PROG := $(subst -cli,,$(BINARY)-$(SAPI_DIRS))$(EXESUFFIX)) $(MAKE) $(SAPI_OBJS) - $(CC) -o $(BUILD_DIR)/$(SAPI_PROG) $(LDFLAGS) $(LIBFLAGS) $(SAPI_OBJS) + $(CC) -o $(BUILD_DIR)/$(SAPI_PROG) $(LDFLAGS) $(SAPI_OBJS) $(LIBFLAGS) $(LIBS) %.test: %.exp %.$(TEST_EXT) @$(MD) ${BUILD_DIR}/${TEST_DIR} diff --git a/engine/lib/memory.c b/engine/lib/memory.c index 01c661e..f3a798b 100644 --- a/engine/lib/memory.c +++ b/engine/lib/memory.c @@ -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) {