Skip to content

Commit

Permalink
FIXED: dict_same_keys/2 could overflow the global stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
JanWielemaker committed Dec 19, 2024
1 parent 1d3f462 commit 915dcce
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/pl-dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ assign_in_dict(DECL_LD Word dp, Word val)
}


#define put_dict(dict, size, nv, new_dict) LDFUNC(put_dict, dict, size, nv, new_dict)
#define put_dict(dict, size, nv, new_dict) \
LDFUNC(put_dict, dict, size, nv, new_dict)

static int
put_dict(DECL_LD word dict, int size, Word nv, word *new_dict)
{ Functor data = valueTerm(dict);
Expand Down Expand Up @@ -439,6 +441,11 @@ put_dict(DECL_LD word dict, int size, Word nv, word *new_dict)
}


/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Push a new dict to the global stack that has the same keys as `dict`,
but whose tag and values are all set to variables.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#define copy_keys_dict(dict, new_dict) \
LDFUNC(copy_keys_dict, dict, new_dict)

Expand All @@ -448,13 +455,13 @@ copy_keys_dict(DECL_LD word dict, word *new_dict)
size_t arity = arityFunctor(data->definition);
Word new, out, in, in_end;

if ( gTop+1 > gMax )
if ( gTop+1+arity > gMax )
return GLOBAL_OVERFLOW;

new = gTop;
out = new;
*out++ = data->definition;
setVar(*out++);
*out++ = data->definition; /* copy C'dict functor */
setVar(*out++); /* set tag to var */
in = data->arguments+1;
in_end = in+arity-1;

Expand Down

0 comments on commit 915dcce

Please sign in to comment.