Skip to content

Commit

Permalink
kernel: replace ImmutableEmptyPlist by a function
Browse files Browse the repository at this point in the history
We need a function NewImmutableEmptyPlist() for creating immutable
plists for now, as unfortunately reusing the same immutable plist
is not safe, as various functions might changes its representation
to something else (in particular: to a string rep), which then
could lead to crashes or worse.
  • Loading branch information
fingolfin committed May 3, 2019
1 parent 7ab3b05 commit c68edd6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/calls.c
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ static Obj FuncCALL_FUNC_LIST_WRAP(Obj self, Obj func, Obj list)

if (retval == 0)
{
retlist = ImmutableEmptyPlist;
retlist = NewImmutableEmptyPlist();
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/listoper.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ static Obj FuncZERO_ATTR_MAT(Obj self, Obj mat)
Obj res;
len = LEN_LIST(mat);
if (len == 0)
return ImmutableEmptyPlist;
return NewImmutableEmptyPlist();
zrow = ZERO(ELM_LIST(mat,1));
CheckedMakeImmutable(zrow);
res = NEW_PLIST_IMM(T_PLIST_TAB_RECT, len);
Expand Down
13 changes: 0 additions & 13 deletions src/plist.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@
#endif


/****************************************************************************
**
*V ImmutableEmptyPlist . . . . . . . . . . . . an immutable empty plain list
**
** 'ImmutableEmptyPlist' is an immutable empty plist.
*/
Obj ImmutableEmptyPlist;

/****************************************************************************
**
*F GROW_PLIST(<list>,<plen>) . . . . make sure a plain list is large enough
Expand Down Expand Up @@ -3307,9 +3299,6 @@ static Int InitKernel (
/* GASMAN marking functions and GASMAN names */
InitBagNamesFromTable( BagNames );

InitGlobalBag(&ImmutableEmptyPlist, "src/plist.c:ImmutableEmptyPlist");


for ( t1 = T_PLIST; t1 < T_PLIST_FFE ; t1 += 2 ) {
InitMarkFuncBags( t1 , MarkAllButFirstSubBags );
InitMarkFuncBags( t1 +IMMUTABLE , MarkAllButFirstSubBags );
Expand Down Expand Up @@ -3721,8 +3710,6 @@ static Int InitLibrary (
InitGVarFiltsFromTable( GVarFilts );
InitGVarFuncsFromTable( GVarFuncs );

ImmutableEmptyPlist = NEW_PLIST_IMM(T_PLIST_EMPTY, 0);

/* return success */
return 0;
}
Expand Down
17 changes: 11 additions & 6 deletions src/plist.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,23 @@ EXPORT_INLINE Obj PopPlist(Obj list)

/****************************************************************************
**
*F NewEmptyPlist() . . . . . . . . . . . . . . create a new empty plain list
*V ImmutableEmptyPlist . . . . . . . . . . . . an immutable empty plain list
**
** ImmutableEmptyPlist is a variable rather than a function, as we can
** reuse the same immutable empty plist.
*F NewEmptyPlist() . . . . . . . . . . create a new mutable empty plain list
*/
EXPORT_INLINE Obj NewEmptyPlist(void)
{
return NEW_PLIST(T_PLIST_EMPTY, 0);
}

extern Obj ImmutableEmptyPlist;

/****************************************************************************
**
*F NewImmutableEmptyPlist() . . . . . create a new immutable empty plain list
*/
EXPORT_INLINE Obj NewImmutableEmptyPlist(void)
{
return NEW_PLIST_IMM(T_PLIST_EMPTY, 0);
}


/****************************************************************************
**
Expand Down
4 changes: 2 additions & 2 deletions src/pperm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static UInt INIT_PPERM(Obj f)
deg = DEG_PPERM<T>(f);

if (deg == 0) {
dom = ImmutableEmptyPlist;
dom = NewImmutableEmptyPlist();
SET_DOM_PPERM(f, dom);
SET_IMG_PPERM(f, dom);
CHANGED_BAG(f);
Expand Down Expand Up @@ -549,7 +549,7 @@ static Obj FuncIMAGE_PPERM(Obj self, Obj f)

UInt rank = RANK_PPERM(f);
if (rank == 0) {
return ImmutableEmptyPlist;
return NewImmutableEmptyPlist();
}

Obj dom = DOM_PPERM(f);
Expand Down
10 changes: 5 additions & 5 deletions src/trans.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ static UInt INIT_TRANS2(Obj f)

if (deg == 0) {
// special case for degree 0
img = ImmutableEmptyPlist;
img = NewImmutableEmptyPlist();
SET_IMG_TRANS(f, img);
SET_KER_TRANS(f, img);
CHANGED_BAG(f);
Expand Down Expand Up @@ -310,7 +310,7 @@ static UInt INIT_TRANS4(Obj f)
// T_TRANS4 and that does not have (internal) degree 65537 or greater
// is ID_TRANS4.

img = ImmutableEmptyPlist;
img = NewImmutableEmptyPlist();
SET_IMG_TRANS(f, img);
SET_KER_TRANS(f, img);
CHANGED_BAG(f);
Expand Down Expand Up @@ -1167,7 +1167,7 @@ static Obj FuncIMAGE_SET_TRANS_INT(Obj self, Obj f, Obj n)
return FuncIMAGE_SET_TRANS(self, f);
}
else if (m == 0) {
return ImmutableEmptyPlist;
return NewImmutableEmptyPlist();
}
else if (m < deg) {
newObj = NEW_PLIST_IMM(T_PLIST_CYC, m);
Expand Down Expand Up @@ -1237,7 +1237,7 @@ static Obj FuncIMAGE_LIST_TRANS_INT(Obj self, Obj f, Obj n)
m = INT_INTOBJ(n);

if (m == 0) {
out = ImmutableEmptyPlist;
out = NewImmutableEmptyPlist();
return out;
}

Expand Down Expand Up @@ -1680,7 +1680,7 @@ static Obj FuncON_KERNEL_ANTI_ACTION(Obj self, Obj ker, Obj f, Obj n)
}

if (len == 0) {
out = ImmutableEmptyPlist;
out = NewImmutableEmptyPlist();
return out;
}
out = NEW_PLIST_IMM(T_PLIST_CYC, len);
Expand Down

0 comments on commit c68edd6

Please sign in to comment.