Skip to content

Commit

Permalink
kernel: also reject invalid AND-filters in compiled code
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Oct 13, 2018
1 parent 27ae3be commit 6d3ba85
Show file tree
Hide file tree
Showing 13 changed files with 1,107 additions and 51 deletions.
27 changes: 18 additions & 9 deletions src/c_oper1.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,17 +890,20 @@ static Obj HdlrFunc3 (
t_4 = (EQ( t_5, t_6 ) ? True : False);
t_3 = t_4;
}
else {
CHECK_FUNC( l_match )
else if (IS_FILTER( l_match ) ) {
C_SUM_FIA( t_8, l_k, l_j )
C_SUM_FIA( t_7, t_8, INTOBJ_INT(1) )
CHECK_INT_POS( t_7 )
C_ELM_LIST_FPL( t_6, l_methods, t_7 )
C_ELM_LIST_FPL( t_7, a_flags, l_j )
t_5 = (EQ( t_6, t_7 ) ? True : False);
CHECK_FUNC( t_5 )
t_3 = NewAndFilter( l_match, t_5 );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(l_match), 0L );
}
l_match = t_3;

}
Expand Down Expand Up @@ -2667,14 +2670,17 @@ static Obj HdlrFunc7 (
CHECK_BOOL( t_6 )
t_5 = t_6;
}
else {
CHECK_FUNC( l_cats )
else if (IS_FILTER( l_cats ) ) {
t_8 = GC_FILTERS;
CHECK_BOUND( t_8, "FILTERS" )
C_ELM_LIST_FPL( t_7, t_8, l_i )
CHECK_FUNC( t_7 )
t_5 = NewAndFilter( l_cats, t_7 );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(l_cats), 0L );
}
l_cats = t_5;

/* rank := rank - RankFilter( FILTERS[i] ); */
Expand Down Expand Up @@ -3201,13 +3207,16 @@ static Obj HdlrFunc15 (
t_3 = (EQ( t_4, a_key ) ? True : False);
t_1 = t_3;
}
else {
CHECK_FUNC( t_2 )
else if (IS_FILTER( t_2 ) ) {
C_ELM_LIST_FPL( t_5, l_known, l_i )
t_4 = (EQ( t_5, a_key ) ? True : False);
CHECK_FUNC( t_4 )
t_1 = NewAndFilter( t_2, t_4 );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(t_2), 0L );
}
RES_BRK_CURR_STAT();
SWITCH_TO_OLD_FRAME(oldFrame);
return t_1;
Expand Down
9 changes: 6 additions & 3 deletions src/c_type1.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,14 @@ static Obj HdlrFunc2 (
CHECK_BOOL( a_tester )
t_5 = a_tester;
}
else {
CHECK_FUNC( t_6 )
CHECK_FUNC( a_tester )
else if (IS_FILTER( t_6 ) ) {
t_5 = NewAndFilter( t_6, a_tester );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(t_6), 0L );
}
SET_ELM_PLIST( t_4, 1, t_5 );
CHANGED_BAG( t_4 );
t_5 = GC_GETTER__FLAGS;
Expand Down
11 changes: 8 additions & 3 deletions src/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1312,13 +1312,18 @@ CVar CompAnd (
Emit( "}\n" );

/* emit the code for the case that the left value is a filter */
Emit( "else {\n" );
CompCheckFunc( left );
Emit( "else if (IS_FILTER( %c ) ) {\n", left );
right2 = CompExpr(READ_EXPR(expr, 1));
CompCheckFunc( right2 );
Emit( "%c = NewAndFilter( %c, %c );\n", val, left, right2 );
Emit( "}\n" );

/* signal an error */
Emit( "else {\n" );
Emit( "ErrorQuit(\n"
"\"<expr> must be 'true' or 'false' or a filter (not a %%s)\",\n"
"(Int)TNAM_OBJ(%c), 0L );\n", left );
Emit( "}\n" );

/* we know precious little about the result */
MergeInfoCVars( INFO_FEXP(CURR_FUNC()), only_left );
SetInfoCVar( val, W_BOUND );
Expand Down
9 changes: 1 addition & 8 deletions src/exprs.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,7 @@ Obj EvalAnd (
else if (IS_FILTER(opL)) {
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
if (IS_FILTER(opR)) {
return NewAndFilter( opL, opR );
}
else {
ErrorQuit(
"<expr> must be a filter (not a %s)",
(Int)TNAM_OBJ(opL), 0L );
}
return NewAndFilter(opL, opR);
}

/* signal an error */
Expand Down
27 changes: 18 additions & 9 deletions src/hpc/c_oper1.c
Original file line number Diff line number Diff line change
Expand Up @@ -922,17 +922,20 @@ static Obj HdlrFunc3 (
t_4 = (EQ( t_5, t_6 ) ? True : False);
t_3 = t_4;
}
else {
CHECK_FUNC( l_match )
else if (IS_FILTER( l_match ) ) {
C_SUM_FIA( t_8, l_k, l_j )
C_SUM_FIA( t_7, t_8, INTOBJ_INT(1) )
CHECK_INT_POS( t_7 )
C_ELM_LIST_FPL( t_6, l_methods, t_7 )
C_ELM_LIST_FPL( t_7, a_flags, l_j )
t_5 = (EQ( t_6, t_7 ) ? True : False);
CHECK_FUNC( t_5 )
t_3 = NewAndFilter( l_match, t_5 );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(l_match), 0L );
}
l_match = t_3;

}
Expand Down Expand Up @@ -2726,14 +2729,17 @@ static Obj HdlrFunc7 (
CHECK_BOOL( t_6 )
t_5 = t_6;
}
else {
CHECK_FUNC( l_cats )
else if (IS_FILTER( l_cats ) ) {
t_8 = GC_FILTERS;
CHECK_BOUND( t_8, "FILTERS" )
C_ELM_LIST_FPL( t_7, t_8, l_i )
CHECK_FUNC( t_7 )
t_5 = NewAndFilter( l_cats, t_7 );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(l_cats), 0L );
}
l_cats = t_5;

/* rank := rank - RankFilter( FILTERS[i] ); */
Expand Down Expand Up @@ -3264,13 +3270,16 @@ static Obj HdlrFunc15 (
t_3 = (EQ( t_4, a_key ) ? True : False);
t_1 = t_3;
}
else {
CHECK_FUNC( t_2 )
else if (IS_FILTER( t_2 ) ) {
C_ELM_LIST_FPL( t_5, l_known, l_i )
t_4 = (EQ( t_5, a_key ) ? True : False);
CHECK_FUNC( t_4 )
t_1 = NewAndFilter( t_2, t_4 );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(t_2), 0L );
}
RES_BRK_CURR_STAT();
SWITCH_TO_OLD_FRAME(oldFrame);
return t_1;
Expand Down
9 changes: 6 additions & 3 deletions src/hpc/c_type1.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,14 @@ static Obj HdlrFunc2 (
CHECK_BOOL( a_tester )
t_5 = a_tester;
}
else {
CHECK_FUNC( t_6 )
CHECK_FUNC( a_tester )
else if (IS_FILTER( t_6 ) ) {
t_5 = NewAndFilter( t_6, a_tester );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(t_6), 0L );
}
SET_ELM_PLIST( t_4, 1, t_5 );
CHANGED_BAG( t_4 );
t_5 = GC_GETTER__FLAGS;
Expand Down
9 changes: 1 addition & 8 deletions src/intrprtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1370,14 +1370,7 @@ void IntrAnd ( void )

/* handle the 'and' of two filters */
else if (IS_FILTER(opL)) {
if (IS_FILTER(opR)) {
PushObj( NewAndFilter( opL, opR ) );
}
else {
ErrorQuit(
"<expr> must be a filter (not a %s)",
(Int)TNAM_OBJ(opL), 0L );
}
PushObj(NewAndFilter(opL, opR));
}

/* signal an error */
Expand Down
8 changes: 8 additions & 0 deletions src/opers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,14 @@ Obj NewAndFilter (
Obj str;
char* s;

if (!IS_FILTER(oper1))
ErrorQuit("<oper1> must be a filter (not a %s)", (Int)TNAM_OBJ(oper1),
0);

if (!IS_FILTER(oper2))
ErrorQuit("<oper2> must be a filter (not a %s)", (Int)TNAM_OBJ(oper2),
0);

if ( oper1 == ReturnTrueFilter )
return oper2;

Expand Down
18 changes: 18 additions & 0 deletions tst/test-compile/and_filter.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
runtest := function()

Print(false and 1, "\n");
Print(true or 1, "\n");
Print(function() return false and 1; end(), "\n");
Print(function() return true or 1; end(), "\n");
Print(IsAssociative and IsAssociative, "\n");

# ensure we don't abort after an error
BreakOnError := false;

# trigger error 1:
CALL_WITH_CATCH({} -> Center and IsAssociative, []);

# trigger error 2:
CALL_WITH_CATCH({} -> IsAssociative and Center, []);

end;
Loading

0 comments on commit 6d3ba85

Please sign in to comment.