Skip to content

Commit

Permalink
ENHANCED: prolog_frame_attribute/3: Better argument validation.
Browse files Browse the repository at this point in the history
Also various minor cleanup (e.g., more `bool`).
  • Loading branch information
JanWielemaker committed Nov 17, 2024
1 parent 9beae60 commit 408f27a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
36 changes: 17 additions & 19 deletions src/pl-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ PL_put_frame(term_t t, LocalFrame fr)
}


static int
static bool
PL_get_frame(term_t r, LocalFrame *fr)
{ GET_LD
intptr_t i;
Expand All @@ -121,18 +121,16 @@ PL_get_frame(term_t r, LocalFrame *fr)
if ( PL_get_intptr(r, &i) )
{ LocalFrame f = ((LocalFrame)((Word)lBase + i));

if ( !(f >= lBase && f < lTop) )
fail;
*fr = f;

succeed;
if ( existingFrame(f) )
{ *fr = f;
return true;
}
} else if ( PL_get_atom(r, &a) && a == ATOM_none )
{ *fr = NULL;

succeed;
return true;
}

fail;
return false;
}


Expand All @@ -149,7 +147,7 @@ PL_put_choice(term_t t, Choice ch)
}


static int
static bool
PL_unify_choice(term_t t, Choice ch)
{ GET_LD

Expand All @@ -163,7 +161,7 @@ PL_unify_choice(term_t t, Choice ch)


#define valid_choice(ch) LDFUNC(valid_choice, ch)
static inline int
static inline bool
valid_choice(DECL_LD Choice ch)
{ if ( (int)ch->type >= 0 && (int)ch->type <= CHP_DEBUG &&
onStack(local, ch->frame) )
Expand All @@ -173,20 +171,20 @@ valid_choice(DECL_LD Choice ch)
}


static int
static bool
PL_get_choice(term_t r, Choice *chp)
{ GET_LD
long i;
intptr_t i;

if ( PL_get_long(r, &i) )
if ( PL_get_intptr(r, &i) )
{ Choice ch = ((Choice)((Word)lBase + i));

if ( !(ch >= (Choice)lBase && ch < (Choice)lTop) ||
!valid_choice(ch) )
return PL_error(NULL, 0, NULL, ERR_EXISTENCE, ATOM_choice, r);
*chp = ch;

succeed;
return true;
} else
return PL_error(NULL, 0, NULL, ERR_TYPE, ATOM_choice, r);
}
Expand All @@ -200,7 +198,7 @@ tracer. `No-debug' code has HIDE_CHILDS. Calls to it must be visible if
the parent is a debug frame.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

int
bool
isDebugFrame(LocalFrame FR)
{ if ( isoff(FR->predicate, TRACE_ME) )
return false; /* hidden predicate */
Expand All @@ -224,7 +222,7 @@ isDebugFrame(LocalFrame FR)
} else
{ QueryFrame qf = queryOfFrame(FR);

return (qf->flags & PL_Q_NODEBUG) ? false : true;
return !(qf->flags & PL_Q_NODEBUG);
}
}

Expand Down Expand Up @@ -1782,8 +1780,8 @@ interruptHandler(int sig)

if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex, PL_FUNCTOR, FUNCTOR_unwind1,
PL_FUNCTOR, FUNCTOR_thread_exit1,
PL_ATOM, ATOM_true) )
PL_FUNCTOR, FUNCTOR_thread_exit1,
PL_ATOM, ATOM_true) )
return;
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/pl-trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#define LDFUNC_DECLARATIONS

void suspendTrace(int suspend); /* suspend/resume tracing */
int isDebugFrame(LocalFrame FR);
bool isDebugFrame(LocalFrame FR);
int tracePort(LocalFrame frame, Choice bfr,
int port, Code PC);
void initTracer(void);
Expand Down
20 changes: 19 additions & 1 deletion src/pl-wam.c
Original file line number Diff line number Diff line change
Expand Up @@ -2521,7 +2521,7 @@ chp_chars(Choice ch)
#endif


int
bool
existingChoice(DECL_LD Choice ch)
{ if ( onStack(local, ch) && onStack(local, ch->frame) &&
(int)ch->type >= 0 && (int)ch->type <= CHP_DEBUG )
Expand All @@ -2537,6 +2537,24 @@ existingChoice(DECL_LD Choice ch)
}


bool
existingFrame(DECL_LD LocalFrame fr)
{ for(;;)
{ if ( !onStack(local, fr) )
return false;
if ( !isFrame(fr) )
return false;

if ( fr->parent )
{ fr = fr->parent;
} else
{ QueryFrame qf = queryOfFrame(fr);
return qf->magic == QID_MAGIC;
}
}
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
discardChoicesAfter() discards all choicepoints created after fr, while
calling possible hooks on the frames. If the `reason` is
Expand Down
4 changes: 3 additions & 1 deletion src/pl-wam.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define PL_next_solution(qid) LDFUNC(PL_next_solution, qid)
#define foreignWakeup(ex) LDFUNC(foreignWakeup, ex)
#define existingChoice(ch) LDFUNC(existingChoice, ch)
#define existingFrame(fr) LDFUNC(existingFrame, fr)
#define grow_trail_ptr(p) LDFUNC(grow_trail_ptr, p)
#define handles_unwind(qid, flags) LDFUNC(handles_unwind, qid, flags)
#endif /*USE_LD_MACROS*/
Expand All @@ -76,7 +77,8 @@ bool raiseSignal(PL_local_data_t *ld, int sig);
int pendingSignal(PL_local_data_t *ld, int sig);
Module contextModule(LocalFrame fr);
void setContextModule(LocalFrame fr, Module context);
int existingChoice(Choice ch);
bool existingChoice(Choice ch);
bool existingFrame(LocalFrame fr);
int grow_trail_ptr(Word p);
bool handles_unwind(qid_t qid, unsigned int flags);
#ifdef O_DEBUG
Expand Down

0 comments on commit 408f27a

Please sign in to comment.