From abcb8f99fee3821798936a9d10cb10e6554f22ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADtor=20Santos=20Costa?= Date: Wed, 24 Sep 2008 00:59:41 +0100 Subject: [PATCH] FIX: InitCPred and friends should check for overflow. --- C/adtdefs.c | 2 ++ C/init.c | 99 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 77 insertions(+), 24 deletions(-) diff --git a/C/adtdefs.c b/C/adtdefs.c index ec6f362cb..adc24e859 100644 --- a/C/adtdefs.c +++ b/C/adtdefs.c @@ -759,6 +759,8 @@ Yap_NewPredPropByAtom(AtomEntry *ae, Term cur_mod) { Prop p0; PredEntry *p = (PredEntry *) Yap_AllocAtomSpace(sizeof(*p)); + if (!p) + return NIL; /* Printf("entering %s:%s/0\n", RepAtom(AtomOfTerm(cur_mod))->StrOfAE, ae->StrOfAE); */ diff --git a/C/init.c b/C/init.c index 69af39f3c..e7f96ad78 100644 --- a/C/init.c +++ b/C/init.c @@ -482,15 +482,28 @@ InitDebug(void) void Yap_InitCPred(char *Name, unsigned long int Arity, CPredicate code, int flags) { - Atom atom = Yap_FullLookupAtom(Name); - PredEntry *pe; + Atom atom = NIL; + PredEntry *pe = NULL; yamop *p_code; StaticClause *cl = NULL; - if (Arity) - pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); - else - pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + while (atom == NIL) { + atom = Yap_FullLookupAtom(Name); + if (atom == NIL && !Yap_growheap(FALSE, 0L, NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } + while (pe == NULL) { + if (Arity) + pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); + else + pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + if (!pe && !Yap_growheap(FALSE, sizeof(PredEntry), NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } if (pe->PredFlags & CPredFlag) { /* already exists */ cl = ClauseCodeToStaticClause(pe->CodeOfPred); @@ -558,16 +571,28 @@ Yap_InitCPred(char *Name, unsigned long int Arity, CPredicate code, int flags) void Yap_InitCmpPred(char *Name, unsigned long int Arity, CmpPredicate cmp_code, int flags) { - Atom atom = Yap_LookupAtom(Name); - PredEntry *pe; + Atom atom = NIL; + PredEntry *pe = NULL; yamop *p_code = NULL; StaticClause *cl = NULL; - if (Arity) { - pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); - } else { - pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + while (atom == NIL) { + atom = Yap_FullLookupAtom(Name); + if (atom == NIL && !Yap_growheap(FALSE, 0L, NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } } + while (pe == NULL) { + if (Arity) + pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); + else + pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + if (!pe && !Yap_growheap(FALSE, sizeof(PredEntry), NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } if (pe->PredFlags & CPredFlag) { p_code = pe->CodeOfPred; /* already exists */ @@ -612,13 +637,26 @@ Yap_InitCmpPred(char *Name, unsigned long int Arity, CmpPredicate cmp_code, int void Yap_InitAsmPred(char *Name, unsigned long int Arity, int code, CPredicate def, int flags) { - Atom atom = Yap_FullLookupAtom(Name); - PredEntry *pe; + Atom atom = NIL; + PredEntry *pe = NULL; - if (Arity) - pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); - else - pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + while (atom == NIL) { + atom = Yap_FullLookupAtom(Name); + if (atom == NIL && !Yap_growheap(FALSE, 0L, NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } + while (pe == NULL) { + if (Arity) + pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); + else + pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + if (!pe && !Yap_growheap(FALSE, sizeof(PredEntry), NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } pe->PredFlags = flags | AsmPredFlag | StandardPredFlag | (code); pe->cs.f_code = def; pe->ModuleOfPred = CurrentModule; @@ -732,13 +770,26 @@ Yap_InitCPredBack(char *Name, unsigned long int Arity, CPredicate Cont, int flags) #endif { - PredEntry *pe; - Atom atom = Yap_FullLookupAtom(Name); + PredEntry *pe = NULL; + Atom atom = NIL; - if (Arity) - pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); - else - pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + while (atom == NIL) { + atom = Yap_FullLookupAtom(Name); + if (atom == NIL && !Yap_growheap(FALSE, 0L, NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } + while (pe == NULL) { + if (Arity) + pe = RepPredProp(PredPropByFunc(Yap_MkFunctor(atom, Arity),CurrentModule)); + else + pe = RepPredProp(PredPropByAtom(atom,CurrentModule)); + if (!pe && !Yap_growheap(FALSE, sizeof(PredEntry), NULL)) { + Yap_Error(OUT_OF_HEAP_ERROR,TermNil,"while initialising %s", Name); + return; + } + } if (pe->cs.p_code.FirstClause != NIL) { #ifdef CUT_C