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

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/at-if-else-if-no-way.zeek, line 12: incorrect use of directive

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/at-if-reject.zeek, line 6: incorrect use of directive

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/at-if-reject.zeek, line 5: incorrect use of directive

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/at-if-reject.zeek, line 5: incorrect use of directive

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/at-if-reject.zeek, line 6: incorrect use of directive

View file

@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/at-if-reject.zeek, line 9: incorrect use of directive
error in <...>/at-if-reject.zeek, line 13: syntax error, at or near "else"

View file

@ -0,0 +1,14 @@
# @TEST-DOC: Regression test for #2289 from vpax - previously this printed "There's way this should happen", now it's a syntax error.
# @TEST-EXEC-FAIL: zeek -b %INPUT
# @TEST-EXEC: btest-diff .stdout
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
event zeek_init()
{
@if ( T )
if ( F )
@else
if ( F )
@endif
print "There's no way this should happen";
}

View file

@ -0,0 +1,57 @@
# @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
}