From 45bef16be0fe9d8b760bc91c463bbcdf9f601b04 Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Tue, 18 Aug 2020 10:31:16 +0000 Subject: [PATCH] Catch if &add_func/&delete_func is applied to IDs that don't have values. We do to allow adding &add_func to a global that's not immediately initialized, as a later redef may fix that. Closes #467. --- src/ID.cc | 7 +++++++ .../Baseline/language.global-unset-addto/output | 2 ++ testing/btest/language/global-unset-addto.zeek | 15 +++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 testing/btest/Baseline/language.global-unset-addto/output create mode 100644 testing/btest/language/global-unset-addto.zeek diff --git a/src/ID.cc b/src/ID.cc index 03f15e1f78..2f9852770d 100644 --- a/src/ID.cc +++ b/src/ID.cc @@ -220,6 +220,13 @@ void ID::SetVal(ExprPtr ev, InitClass c) if ( ! a ) Internal("no add/delete function in ID::SetVal"); + if ( ! val ) + { + Error(fmt("%s initializer applied to ID without value", + (c == INIT_EXTRA ? "+=" : "-="), this); + return; + } + EvalFunc(a->GetExpr(), std::move(ev)); } diff --git a/testing/btest/Baseline/language.global-unset-addto/output b/testing/btest/Baseline/language.global-unset-addto/output new file mode 100644 index 0000000000..306f0afddb --- /dev/null +++ b/testing/btest/Baseline/language.global-unset-addto/output @@ -0,0 +1,2 @@ +error in /Users/robin/bro/master/testing/btest/.tmp/language.global-unset-addto/global-unset-addto.zeek, line 4: += initializer applied to ID without value (a) +error in /Users/robin/bro/master/testing/btest/.tmp/language.global-unset-addto/global-unset-addto.zeek, line 5: -= initializer applied to ID without value (b) diff --git a/testing/btest/language/global-unset-addto.zeek b/testing/btest/language/global-unset-addto.zeek new file mode 100644 index 0000000000..bf2f794809 --- /dev/null +++ b/testing/btest/language/global-unset-addto.zeek @@ -0,0 +1,15 @@ +# @TEST-EXEC-FAIL: zeek -b %INPUT >output 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output + +global a: count &add_func = function(old: count, new: count): count { return 3; }; +global b: count &delete_func = function(old: count, new: count): count { return 3; }; + +redef a += 13; +redef b -= 13; + +# The following is ok. +global c: count &redef &add_func = function(old: count, new: count): count { return 3; }; +redef c = 0; +redef c += 13; + +