parse.y/directives: Reject directives as statements

Avoid the issue outlined in #2289 where the @if or @else is taken as the
statement of an `if`, `for` or `while` by rejecting such constructs.

Effectively this means the following scripts are now rejected:

    # Print's "cond true" with Zeek 5.0 even though the `if ( F )`
    # should be in effect.

    if ( F )
        @if ( T )
            print "cond true";
        @else
            print "cond false";
        @endif

or

    # Print's "hello" once with Zeek 5.0
    local v = vector( 1, 2, 3 );

    for ( i in v )
        @if ( T )
        print("hello")
        @endif

To make above work as intended, additional braces can be used.

    if ( T )
        {
    @if ( cond )
            print "cond true";
    @else
            print "cond false";
    @endif
        }

    for ( i in v )
        {
    @if ( T )
        print("hello")
    @endif
        }
This commit is contained in:
Arne Welzel 2022-08-23 18:45:22 +02:00
parent 6721248da5
commit 171846a37a
16 changed files with 124 additions and 2 deletions

View file

@ -158,6 +158,12 @@ const ReturnStmt* Stmt::AsReturnStmt() const
return (const ReturnStmt*)this;
}
const NullStmt* Stmt::AsNullStmt() const
{
CHECK_TAG(tag, STMT_NULL, "Stmt::AsNullStmt", stmt_name)
return (const NullStmt*)this;
}
bool Stmt::SetLocationInfo(const Location* start, const Location* end)
{
if ( ! Obj::SetLocationInfo(start, end) )
@ -1785,6 +1791,8 @@ TraversalCode InitStmt::Traverse(TraversalCallback* cb) const
HANDLE_TC_STMT_POST(tc);
}
NullStmt::NullStmt(bool arg_is_directive) : Stmt(STMT_NULL), is_directive(arg_is_directive) { }
ValPtr NullStmt::Exec(Frame* /* f */, StmtFlowType& flow)
{
RegisterAccess();