From d48345a9eaf9db8e0ba291a5c21ec6175f9b63ba Mon Sep 17 00:00:00 2001 From: "zik.saleeba" Date: Wed, 11 Mar 2009 01:59:22 +0000 Subject: [PATCH] Safety check for bucket sizes git-svn-id: http://picoc.googlecode.com/svn/trunk@191 21eae674-98b7-11dd-bd71-f92a316d2d60 --- heap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/heap.c b/heap.c index c10f6fc..65ae98d 100644 --- a/heap.c +++ b/heap.c @@ -94,13 +94,18 @@ void *HeapAlloc(int Size) struct AllocNode *NewMem = NULL; struct AllocNode **FreeNode; int AllocSize = MEM_ALIGN(Size) + sizeof(NewMem->Size); - int Bucket = AllocSize >> 2; + int Bucket; if (Size == 0) return NULL; assert(Size > 0); + /* make sure we have enough space for an AllocNode */ + if (AllocSize < sizeof(struct AllocNode)) + AllocSize = sizeof(struct AllocNode); + + Bucket = AllocSize >> 2; if (Bucket < FREELIST_BUCKETS && FreeListBucket[Bucket] != NULL) { /* try to allocate from a freelist bucket first */ #ifdef DEBUG_HEAP