Return an error if redef'ing a variable without redef attribute

This commit is contained in:
Tim Wojtulewicz 2022-07-21 13:58:00 -07:00
parent 86c316ddfc
commit 00e111135b
4 changed files with 39 additions and 0 deletions

View file

@ -223,6 +223,12 @@ static void make_var(const IDPtr& id, TypePtr t, InitClass c, ExprPtr init,
id->Warn("redefinition requires \"redef\"", redef_obj, true);
}
else if ( dt == VAR_REDEF && ! id->IsRedefinable() )
{
id->Error("cannot redefine a variable not marked with &redef", init.get());
return;
}
else if ( dt != VAR_REDEF || init || ! attr )
{
if ( IsFunc(id->GetType()->Tag()) )

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 <...>/redef-global-function.zeek, line 22 and <command line>, line 1: cannot redefine a variable not marked with &redef (funca)

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.
func2()

View file

@ -0,0 +1,29 @@
# @TEST-EXEC: zeek -b %INPUT -e "redef funcb = func2;" > out
# @TEST-EXEC-FAIL: zeek -b %INPUT -e "redef funca = func2;" >> out
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
export {
global func1: function();
global func2: function();
}
function func1()
{
print "func1()";
}
function func2()
{
print "func2()";
}
export {
global funca = func1;
global funcb = func1 &redef;
}
event zeek_init()
{
funcb();
}