From b86d5b4424baba75fae9c528c21a81a99643b0aa Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sat, 25 Apr 2020 16:56:24 -0700 Subject: [PATCH] bug fixes for using "when" in functions that have a local of type "any" --- src/Type.cc | 7 ++++++- src/Type.h | 3 ++- src/Val.cc | 4 ++++ testing/btest/Baseline/language.any-when/out | 1 + testing/btest/language/any-when.zeek | 19 +++++++++++++++++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 testing/btest/Baseline/language.any-when/out create mode 100644 testing/btest/language/any-when.zeek diff --git a/src/Type.cc b/src/Type.cc index 2683356722..1143093789 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -1615,7 +1615,12 @@ bool same_type(const BroType* t1, const BroType* t2, bool is_init, bool match_re } case TYPE_TYPE: - return same_type(t1, t2, is_init, match_record_field_names); + { + auto tt1 = t1->AsTypeType(); + auto tt2 = t2->AsTypeType(); + return same_type(tt1->Type(), tt1->Type(), + is_init, match_record_field_names); + } case TYPE_UNION: reporter->Error("union type in same_type()"); diff --git a/src/Type.h b/src/Type.h index a47239ac2d..daa6a7fb13 100644 --- a/src/Type.h +++ b/src/Type.h @@ -506,7 +506,8 @@ public: explicit TypeType(IntrusivePtr t) : BroType(TYPE_TYPE), type(std::move(t)) {} TypeType* ShallowClone() override { return new TypeType(type); } - BroType* Type() { return type.get(); } + BroType* Type() { return type.get(); } + const BroType* Type() const { return type.get(); } protected: IntrusivePtr type; diff --git a/src/Val.cc b/src/Val.cc index 6c24a89471..a67ad32efd 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -136,6 +136,10 @@ IntrusivePtr Val::DoClone(CloneState* state) return {NewRef{}, this}; } + if ( type->Tag() == TYPE_TYPE ) + // These are immutable, essentially. + return {NewRef{}, this}; + // Fall-through. default: diff --git a/testing/btest/Baseline/language.any-when/out b/testing/btest/Baseline/language.any-when/out new file mode 100644 index 0000000000..ec635144f6 --- /dev/null +++ b/testing/btest/Baseline/language.any-when/out @@ -0,0 +1 @@ +9 diff --git a/testing/btest/language/any-when.zeek b/testing/btest/language/any-when.zeek new file mode 100644 index 0000000000..8ce99da844 --- /dev/null +++ b/testing/btest/language/any-when.zeek @@ -0,0 +1,19 @@ +# @TEST-EXEC: zeek -b %INPUT >out +# @TEST-EXEC: btest-diff out + +type c: count; +function foo(): count + { + local bar: any; + bar = c; + return when ( 5 > 3 ) + { + return 9; + } + } + +event zeek_init() + { + when ( local b = foo() ) + print b; + }