From 2e6360738ca894b82aad71a101caf520be626004 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 14 Dec 2012 19:35:26 +0000 Subject: [PATCH] itry to use size_t fix overflow handling bugs. --- C/alloc.c | 8 ++++---- C/cdmgr.c | 14 +++++++------- C/grow.c | 4 ++-- H/Yapproto.h | 8 ++++---- H/alloc.h | 4 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/C/alloc.c b/C/alloc.c index 152aec116..5b4221a3e 100644 --- a/C/alloc.c +++ b/C/alloc.c @@ -126,7 +126,7 @@ long long unsigned int tmalloc; static inline char * -call_malloc(unsigned long int size) +call_malloc(size_t size) { CACHE_REGS char *out; @@ -152,7 +152,7 @@ call_malloc(unsigned long int size) } char * -Yap_AllocCodeSpace(unsigned long int size) +Yap_AllocCodeSpace(size_t size) { size = AdjustSize(size); return call_malloc(size); @@ -187,7 +187,7 @@ call_realloc(char *p, unsigned long int size) } char * -Yap_ReallocCodeSpace(char *p, unsigned long int size) +Yap_ReallocCodeSpace(char *p, size_t size) { size = AdjustSize(size); return call_realloc(p, size); @@ -214,7 +214,7 @@ Yap_FreeCodeSpace(char *p) } char * -Yap_AllocAtomSpace(unsigned long int size) +Yap_AllocAtomSpace(size_t size) { size = AdjustSize(size); return call_malloc(size); diff --git a/C/cdmgr.c b/C/cdmgr.c index 571044842..ac0cc6161 100644 --- a/C/cdmgr.c +++ b/C/cdmgr.c @@ -809,7 +809,7 @@ Yap_BuildMegaClause(PredEntry *ap) UInt sz; MegaClause *mcl; yamop *ptr; - UInt required; + size_t required; UInt has_blobs = 0; if (ap->PredFlags & (DynamicPredFlag|LogUpdatePredFlag|MegaClausePredFlag @@ -839,6 +839,12 @@ Yap_BuildMegaClause(PredEntry *ap) sz -= (UInt)NEXTOP((yamop *)NULL,p) + sizeof(StaticClause); } required = sz*ap->cs.p_code.NOfClauses+sizeof(MegaClause)+(UInt)NEXTOP((yamop *)NULL,l); + while (!(mcl = (MegaClause *)Yap_AllocCodeSpace(required))) { + if (!Yap_growheap(FALSE, required, NULL)) { + /* just fail, the system will keep on going */ + return; + } + } #ifdef DEBUG total_megaclause += required; cl = @@ -846,12 +852,6 @@ Yap_BuildMegaClause(PredEntry *ap) total_released += ap->cs.p_code.NOfClauses*cl->ClSize; nof_megaclauses++; #endif - while (!(mcl = (MegaClause *)Yap_AllocCodeSpace(required))) { - if (!Yap_growheap(FALSE, required, NULL)) { - /* just fail, the system will keep on going */ - return; - } - } Yap_ClauseSpace += required; /* cool, it's our turn to do the conversion */ mcl->ClFlags = MegaMask | has_blobs; diff --git a/C/grow.c b/C/grow.c index 76c470b8e..eb53ba5ce 100644 --- a/C/grow.c +++ b/C/grow.c @@ -1410,7 +1410,7 @@ growatomtable( USES_REGS1 ) int -Yap_growheap(int fix_code, UInt in_size, void *cip) +Yap_growheap(int fix_code, size_t in_size, void *cip) { CACHE_REGS int res; @@ -1441,7 +1441,7 @@ Yap_growheap(int fix_code, UInt in_size, void *cip) } #if USE_SYSTEM_MALLOC P = Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"malloc failed"); - res = -1; + res = FALSE; #else res=do_growheap(fix_code, in_size, (struct intermediates *)cip, NULL, NULL, NULL PASS_REGS); #endif diff --git a/H/Yapproto.h b/H/Yapproto.h index ef9a217cc..bf383af56 100644 --- a/H/Yapproto.h +++ b/H/Yapproto.h @@ -83,9 +83,9 @@ void STD_PROTO(Yap_init_agc, (void)); /* alloc.c */ void STD_PROTO(Yap_FreeCodeSpace,(char *)); -char *STD_PROTO(Yap_AllocAtomSpace,(unsigned long int)); -char *STD_PROTO(Yap_AllocCodeSpace,(unsigned long int)); -char *STD_PROTO(Yap_ReallocCodeSpace,(char *,unsigned long int)); +char *STD_PROTO(Yap_AllocAtomSpace,(size_t)); +char *STD_PROTO(Yap_AllocCodeSpace,(size_t)); +char *STD_PROTO(Yap_ReallocCodeSpace,(char *,size_t)); ADDR STD_PROTO(Yap_AllocFromForeignArea,(Int)); int STD_PROTO(Yap_ExtendWorkSpace,(Int)); void STD_PROTO(Yap_FreeAtomSpace,(char *)); @@ -206,7 +206,7 @@ void STD_PROTO(Yap_AllocateDefaultArena, (Int, Int, int)); Int STD_PROTO(Yap_total_stack_shift_time,(void)); void STD_PROTO(Yap_InitGrowPreds, (void)); UInt STD_PROTO(Yap_InsertInGlobal, (CELL *, UInt)); -int STD_PROTO(Yap_growheap, (int, UInt, void *)); +int STD_PROTO(Yap_growheap, (int, size_t, void *)); int STD_PROTO(Yap_growstack, (long)); int STD_PROTO(Yap_growtrail, (long, int)); int STD_PROTO(Yap_growglobal, (CELL **)); diff --git a/H/alloc.h b/H/alloc.h index 7b624e656..56cc32a68 100644 --- a/H/alloc.h +++ b/H/alloc.h @@ -66,10 +66,10 @@ typedef struct FREEB { #if SIZEOF_INT_P==4 #define YAP_ALIGN 3 -#define YAP_ALIGNMASK 0xfffffffc +#define YAP_ALIGNMASK ((CELL)(-4)) #else #define YAP_ALIGN 7 -#define YAP_ALIGNMASK 0xfffffff8L +#define YAP_ALIGNMASK ((CELL)(-8)) #endif /* ALIGN_LONGS */ #define AdjustSize(X) ((X+YAP_ALIGN) & YAP_ALIGNMASK)