Added interpreter error for local event variables.

Scheduling a local event variable resulted in a global lookup instead of
evaluating the local variable. To prevent misunderstandings, this will
trigger an error now.
This commit is contained in:
Jan Grashoefer 2016-05-11 12:26:11 +02:00
parent cc54b3772a
commit 65607239c9
5 changed files with 35 additions and 7 deletions

View file

@ -1474,11 +1474,20 @@ event:
TOK_ID '(' opt_expr_list ')' TOK_ID '(' opt_expr_list ')'
{ {
set_location(@1, @4); set_location(@1, @4);
$$ = new EventExpr($1, $3);
ID* id = lookup_ID($1, current_module.c_str());
if ( id && id->IsDeprecated() ) ID* id = lookup_ID($1, current_module.c_str());
reporter->Warning("deprecated (%s)", id->Name()); if ( id )
{
if ( ! id->IsGlobal() )
{
yyerror(fmt("local identifier \"%s\" cannot be used to reference an event", $1));
YYERROR;
}
if ( id->IsDeprecated() )
reporter->Warning("deprecated (%s)", id->Name());
}
$$ = new EventExpr($1, $3);
} }
; ;

View file

@ -0,0 +1 @@
error in /home/jgras/devel/bro/testing/btest/.tmp/language.event-local-var/event-local-var.bro, line 15: local identifier "v" cannot be used to reference an event, at or near ")"

View file

@ -1,6 +1,7 @@
event statement event statement
event part1 event part1
event part2 event part2
assign event variable (6)
schedule statement in bro_init schedule statement in bro_init
schedule statement in global schedule statement in global
schedule statement another in bro_init schedule statement another in bro_init

View file

@ -0,0 +1,16 @@
# @TEST-EXEC-FAIL: bro -b %INPUT &> out
# @TEST-EXEC: btest-diff out
event e1(num: count)
{
print fmt("event 1: %s", num);
}
event bro_init()
{
# Test assigning a local event variable to an event
local v: event(num: count);
v = e1;
schedule 1sec { v(6) }; # This should fail
}

View file

@ -21,7 +21,7 @@ event e3(test: string)
event e4(num: count) event e4(num: count)
{ {
print "assign event variable"; print fmt("assign event variable (%s)", num);
} }
# Note: the name of this event is intentionally the same as one above # Note: the name of this event is intentionally the same as one above
@ -30,6 +30,8 @@ event e3(test: string)
print "event part2"; print "event part2";
} }
global e5: event(num: count);
event bro_init() event bro_init()
{ {
# Test calling an event with "event" statement # Test calling an event with "event" statement
@ -43,9 +45,8 @@ event bro_init()
event e3("foo"); event e3("foo");
# Test assigning an event variable to an event # Test assigning an event variable to an event
local e5: event(num: count);
e5 = e4; e5 = e4;
event e5(6); # TODO: this does not do anything event e5(6);
} }
# scheduling in outside of an event handler shouldn't crash. # scheduling in outside of an event handler shouldn't crash.