zeek/testing/btest/language/at-if-reject.zeek
Arne Welzel 171846a37a 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
        }
2022-08-26 09:42:50 +02:00

57 lines
908 B
Text

# @TEST-DOC: Test for #2289 - reject directives appearing as statements
# @TEST-EXEC-FAIL: zeek -b %INPUT
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
event zeek_init()
{
if ( F )
@if ( T )
print "Bad branch true";
@else
print "Bad branch false";
@endif
else
print "That's the right branch";
}
@TEST-START-NEXT
event zeek_init()
{
if ( F )
print "That would be okay";
else
@if ( T )
print "That isn't";
@endif
}
@TEST-START-NEXT
event zeek_init()
{
local vec = vector(1, 2, 3);
for ( i in vec )
@if ( T )
print "Bad branch true";
@endif
}
@TEST-START-NEXT
event zeek_init()
{
local i = 10;
while ( --i != 0 )
@if ( T )
print "Bad branch true";
@endif
}
@TEST-START-NEXT
global cond = T;
event zeek_init()
{
local vec = vector(1, 2, 3);
for ( i in vec )
@if ( cond )
print "Bad branch true";
@endif
}