From c68edd6f06374d2e9091c7bd140fb8c206222647 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 2 May 2019 11:36:16 +0200 Subject: [PATCH] kernel: replace ImmutableEmptyPlist by a function 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. --- src/calls.c | 2 +- src/listoper.c | 2 +- src/plist.c | 13 ------------- src/plist.h | 17 +++++++++++------ src/pperm.cc | 4 ++-- src/trans.cc | 10 +++++----- 6 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/calls.c b/src/calls.c index d2694d39ba..7667755b8c 100644 --- a/src/calls.c +++ b/src/calls.c @@ -1205,7 +1205,7 @@ static Obj FuncCALL_FUNC_LIST_WRAP(Obj self, Obj func, Obj list) if (retval == 0) { - retlist = ImmutableEmptyPlist; + retlist = NewImmutableEmptyPlist(); } else { diff --git a/src/listoper.c b/src/listoper.c index 5f561c5499..161cf7451d 100644 --- a/src/listoper.c +++ b/src/listoper.c @@ -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); diff --git a/src/plist.c b/src/plist.c index 81b2e82576..795d6bdb7f 100644 --- a/src/plist.c +++ b/src/plist.c @@ -59,14 +59,6 @@ #endif -/**************************************************************************** -** -*V ImmutableEmptyPlist . . . . . . . . . . . . an immutable empty plain list -** -** 'ImmutableEmptyPlist' is an immutable empty plist. -*/ -Obj ImmutableEmptyPlist; - /**************************************************************************** ** *F GROW_PLIST(,) . . . . make sure a plain list is large enough @@ -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 ); @@ -3721,8 +3710,6 @@ static Int InitLibrary ( InitGVarFiltsFromTable( GVarFilts ); InitGVarFuncsFromTable( GVarFuncs ); - ImmutableEmptyPlist = NEW_PLIST_IMM(T_PLIST_EMPTY, 0); - /* return success */ return 0; } diff --git a/src/plist.h b/src/plist.h index 70da69109d..9021b3ab3f 100644 --- a/src/plist.h +++ b/src/plist.h @@ -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); +} + /**************************************************************************** ** diff --git a/src/pperm.cc b/src/pperm.cc index ddf67e7ad8..3613fe80aa 100644 --- a/src/pperm.cc +++ b/src/pperm.cc @@ -278,7 +278,7 @@ static UInt INIT_PPERM(Obj f) deg = DEG_PPERM(f); if (deg == 0) { - dom = ImmutableEmptyPlist; + dom = NewImmutableEmptyPlist(); SET_DOM_PPERM(f, dom); SET_IMG_PPERM(f, dom); CHANGED_BAG(f); @@ -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); diff --git a/src/trans.cc b/src/trans.cc index 6eb00c6d5f..ada00b16ad 100644 --- a/src/trans.cc +++ b/src/trans.cc @@ -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); @@ -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); @@ -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); @@ -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; } @@ -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);