From 4f566f35ee0e9ef83f881b0054784f9eb8612ff4 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Wed, 22 Dec 2021 14:26:03 -0800 Subject: [PATCH] bug fix for reporting poorly formed record constructors --- src/Expr.cc | 9 ++++++++- .../btest/Baseline/language.record-bad-ctor3/out | 2 ++ testing/btest/language/record-bad-ctor3.zeek | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 testing/btest/Baseline/language.record-bad-ctor3/out create mode 100644 testing/btest/language/record-bad-ctor3.zeek diff --git a/src/Expr.cc b/src/Expr.cc index 9271cf7e22..c291f288ae 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -3307,11 +3307,15 @@ RecordConstructorExpr::RecordConstructorExpr(ListExprPtr constructor_list) const ExprPList& exprs = op->AsListExpr()->Exprs(); type_decl_list* record_types = new type_decl_list(exprs.length()); + const Expr* constructor_error_expr = nullptr; + for ( const auto& e : exprs ) { if ( e->Tag() != EXPR_FIELD_ASSIGN ) { - Error("bad type in record constructor", e); + // Don't generate the error yet, as reporting it + // requires that we have a well-formed type. + constructor_error_expr = e; SetError(); continue; } @@ -3323,6 +3327,9 @@ RecordConstructorExpr::RecordConstructorExpr(ListExprPtr constructor_list) } SetType(make_intrusive(record_types)); + + if ( constructor_error_expr ) + Error("bad type in record constructor", constructor_error_expr); } RecordConstructorExpr::RecordConstructorExpr(RecordTypePtr known_rt, ListExprPtr constructor_list) diff --git a/testing/btest/Baseline/language.record-bad-ctor3/out b/testing/btest/Baseline/language.record-bad-ctor3/out new file mode 100644 index 0000000000..4903963619 --- /dev/null +++ b/testing/btest/Baseline/language.record-bad-ctor3/out @@ -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 <...>/record-bad-ctor3.zeek, line 13: bad type in record constructor ([$x=a + 5, a + 9] and a + 9) diff --git a/testing/btest/language/record-bad-ctor3.zeek b/testing/btest/language/record-bad-ctor3.zeek new file mode 100644 index 0000000000..539aa5361a --- /dev/null +++ b/testing/btest/language/record-bad-ctor3.zeek @@ -0,0 +1,15 @@ +# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1 +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out + +# Every element in a record ctor's expression list should have an assignment +# form. Make sure we correctly report errors when that's not the case. + +global a = 3; + +type r: record { x: count; y: count; }; + +event zeek_init() + { + local b: r = record($x = a + 5, a + 9); + print b; + }