Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checking to weakptr functions #1009

Merged
merged 1 commit into from
Dec 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 77 additions & 2 deletions src/weakptr.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ Int LengthWPObj(Obj wp)

Obj FuncLengthWPObj(Obj self, Obj wp)
{
if (TNUM_OBJ(wp) != T_WPOBJ)
{
ErrorMayQuit("LengthWPObj: argument must be a weak pointer object, not a %s",
(Int)TNAM_OBJ(wp), 0);
}
return INTOBJ_INT(LengthWPObj(wp));
}

Expand All @@ -216,7 +221,24 @@ Obj FuncLengthWPObj(Obj self, Obj wp)

Obj FuncSetElmWPObj(Obj self, Obj wp, Obj pos, Obj val)
{
if (TNUM_OBJ(wp) != T_WPOBJ)
{
ErrorMayQuit("SetElmWPObj: First argument must be a weak pointer object, not a %s",
(Int)TNAM_OBJ(wp), 0);
}

if (!IS_INTOBJ(pos))
{
ErrorMayQuit("SetElmWPObj: Position must be a small integer, not a %s",
(Int)TNAM_OBJ(pos),0L);
}

UInt ipos = INT_INTOBJ(pos);
if (ipos < 1)
{
ErrorMayQuit("SetElmWPObj: Position must be a positive integer",0L,0L);
}

if (LengthWPObj(wp) < ipos)
{
GROW_WPOBJ(wp, ipos);
Expand All @@ -239,7 +261,24 @@ Obj FuncSetElmWPObj(Obj self, Obj wp, Obj pos, Obj val)

Int IsBoundElmWPObj( Obj wp, Obj pos)
{
if (TNUM_OBJ(wp) != T_WPOBJ)
{
ErrorMayQuit("IsBoundElmWPObj: First argument must be a weak pointer object, not a %s",
(Int)TNAM_OBJ(wp), 0);
}

if (!IS_INTOBJ(pos))
{
ErrorMayQuit("IsBoundElmWPObj: Position must be a small integer, not a %s",
(Int)TNAM_OBJ(pos),0L);
}

UInt ipos = INT_INTOBJ(pos);
if (ipos < 1)
{
ErrorMayQuit("IsBoundElmWPObj: Position must be a positive integer",0L,0L);
}

Obj elm;
if ( LengthWPObj(wp) < ipos )
{
Expand Down Expand Up @@ -282,9 +321,27 @@ Obj FuncIsBoundElmWPObj( Obj self, Obj wp, Obj pos)

Obj FuncUnbindElmWPObj( Obj self, Obj wp, Obj pos)
{
if (TNUM_OBJ(wp) != T_WPOBJ)
{
ErrorMayQuit("UnbindElmWPObj: First argument must be a weak pointer object, not a %s",
(Int)TNAM_OBJ(wp), 0);
}

if (!IS_INTOBJ(pos))
{
ErrorMayQuit("UnbindElmWPObj: Position must be a small integer, not a %s",
(Int)TNAM_OBJ(pos),0L);
}

UInt ipos = INT_INTOBJ(pos);
if (ipos < 1)
{
ErrorMayQuit("UnbindElmWPObj: Position must be a positive integer",0L,0L);
}

Int len = LengthWPObj(wp);
if ( INT_INTOBJ(pos) <= len ) {
ELM_WPOBJ( wp, INT_INTOBJ(pos)) = 0;
if ( ipos <= len ) {
ELM_WPOBJ( wp, ipos) = 0;
}
return 0;
}
Expand All @@ -306,7 +363,25 @@ Obj FuncUnbindElmWPObj( Obj self, Obj wp, Obj pos)
Obj FuncElmWPObj( Obj self, Obj wp, Obj pos)
{
Obj elm;

if (TNUM_OBJ(wp) != T_WPOBJ)
{
ErrorMayQuit("ElmWPObj: First argument must be a weak pointer object, not a %s",
(Int)TNAM_OBJ(wp), 0);
}

if (!IS_INTOBJ(pos))
{
ErrorMayQuit("ElmWPObj: Position must be a small integer, not a %s",
(Int)TNAM_OBJ(pos),0L);
}

UInt ipos = INT_INTOBJ(pos);
if (ipos < 1)
{
ErrorMayQuit("ElmWPObj: Position must be a positive integer",0L,0L);
}

if ( STORED_LEN_WPOBJ(wp) < ipos )
{
return Fail;
Expand Down
46 changes: 46 additions & 0 deletions tst/testinstall/weakptr-badargs.tst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#############################################################################
gap> START_TEST("weakptr-badargs.tst");
gap> w := WeakPointerObject([1,,3,4]);
Error, Variable: 'WeakPointerObject' must have a value
gap> w := WeakPointerObj([1,,3,4]);;
gap> SetElmWPObj(w, 0, 0);
Error, SetElmWPObj: Position must be a positive integer
gap> SetElmWPObj(w, [1,2], 0);
Error, SetElmWPObj: Position must be a small integer, not a list (plain,cyc)
gap> SetElmWPObj(w, (), 0);
Error, SetElmWPObj: Position must be a small integer, not a permutation (small\
)
gap> SetElmWPObj((), 1, 1);
Error, SetElmWPObj: First argument must be a weak pointer object, not a permut\
ation (small)
gap> UnbindElmWPObj(w, 0);
Error, UnbindElmWPObj: Position must be a positive integer
gap> UnbindElmWPObj(w, []);
Error, UnbindElmWPObj: Position must be a small integer, not a list (plain,emp\
ty)
gap> UnbindElmWPObj([], 2);
Error, UnbindElmWPObj: First argument must be a weak pointer object, not a lis\
t (plain,empty)
gap> ElmWPObj(w, 0);
Error, ElmWPObj: Position must be a positive integer
gap> ElmWPObj(w, []);
Error, ElmWPObj: Position must be a small integer, not a list (plain,empty)
gap> ElmWPObj([], 1);
Error, ElmWPObj: First argument must be a weak pointer object, not a list (pla\
in,empty)
gap> IsBoundWPObj(w, 0);
Error, Variable: 'IsBoundWPObj' must have a value
gap> IsBoundElmWPObj(w, 0);
Error, IsBoundElmWPObj: Position must be a positive integer
gap> IsBoundElmWPObj(w, []);
Error, IsBoundElmWPObj: Position must be a small integer, not a list (plain,em\
pty)
gap> IsBoundElmWPObj([], 1);
Error, IsBoundElmWPObj: First argument must be a weak pointer object, not a li\
st (plain,empty)
gap> LengthWPObj([]);
Error, LengthWPObj: argument must be a weak pointer object, not a list (plain,\
empty)
gap> LengthWPObj(0);
Error, LengthWPObj: argument must be a weak pointer object, not a integer
gap> STOP_TEST( "weakptr-badargs.tst", 100);