From 12add531313c957edbb5f28f1cbb768e184e40d4 Mon Sep 17 00:00:00 2001 From: Johanna Amann Date: Fri, 20 Jul 2018 13:36:38 -0700 Subject: [PATCH] Fix special-case-bug for vectors in UnaryExpr. In some cases one can get the Type() of unaryexpr to be ANY. Vectors so far did not deal gracefully with this and crashed because trying to convert any to a vectortype. This patch fixes this by just using the original vector-type in this case. --- src/Expr.cc | 8 +++++++- testing/btest/core/vector-assignment.bro | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 testing/btest/core/vector-assignment.bro diff --git a/src/Expr.cc b/src/Expr.cc index 4c3c7a536a..a86f86b9c4 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -456,7 +456,13 @@ Val* UnaryExpr::Eval(Frame* f) const if ( is_vector(v) ) { VectorVal* v_op = v->AsVectorVal(); - VectorVal* result = new VectorVal(Type()->AsVectorType()); + VectorType* out_t; + if ( Type()->Tag() == TYPE_ANY ) + out_t = v->Type()->AsVectorType(); + else + out_t = Type()->AsVectorType(); + + VectorVal* result = new VectorVal(out_t); for ( unsigned int i = 0; i < v_op->Size(); ++i ) { diff --git a/testing/btest/core/vector-assignment.bro b/testing/btest/core/vector-assignment.bro new file mode 100644 index 0000000000..d1f02c124f --- /dev/null +++ b/testing/btest/core/vector-assignment.bro @@ -0,0 +1,19 @@ +# @TEST-EXEC: bro %INPUT + +# This regression test checks a special case in the vector code. In this case +# UnaryExpr will be called with a Type() of any. Tests succeeds if it does not +# crash Bro. + +type OptionCacheValue: record { + val: any; +}; + +function set_me(val: any) { + local a = OptionCacheValue($val=val); + print a; +} + +event bro_init() { + local b: vector of count = {1, 2, 3}; + set_me(b); +}