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

kernel: reject invalid AND-filters #2756

Merged
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
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
13 changes: 3 additions & 10 deletions src/exprs.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,23 +183,16 @@ Obj EvalAnd (
}

/* handle the 'and' of two filters */
else if ( TNUM_OBJ(opL) == T_FUNCTION ) {
else if (IS_FILTER(opL)) {
tmp = READ_EXPR(expr, 1);
opR = EVAL_EXPR( tmp );
if ( TNUM_OBJ(opR) == T_FUNCTION ) {
return NewAndFilter( opL, opR );
}
else {
ErrorQuit(
"<expr> must be 'true' or 'false' (not a %s)",
(Int)TNAM_OBJ(opL), 0L );
}
return NewAndFilter(opL, opR);
}

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

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
13 changes: 3 additions & 10 deletions src/intrprtr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1369,21 +1369,14 @@ void IntrAnd ( void )
}

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

/* signal an error */
else {
ErrorQuit(
"<expr> must be 'true' or 'false' (not a %s)",
"<expr> must be 'true' or 'false' or a filter (not a %s)",
(Int)TNAM_OBJ(opL), 0L );
}
}
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
1 change: 1 addition & 0 deletions src/opers.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ static inline Int IS_FILTER(Obj oper)
return v & OPER_IS_FILTER;
}


/****************************************************************************
**
*F SET_IS_FILTER( <oper> ) . . . . . . . . . . . mark operation as a filter
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