diff --git a/CHANGES b/CHANGES index 32a05e1e5f..077b0e452f 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,9 @@ +7.1.0-dev.40 | 2024-07-24 11:18:03 +0200 + + * minor optimization of boolean comparisons (Vern Paxson, Corelight) + + * GH-3839: fix & regression test for GH-3839 (spurious warnings for "when" constructs) (Vern Paxson, Corelight) + 7.1.0-dev.37 | 2024-07-23 19:18:37 -0700 * Bump zeek-testing-cluster to reflect deprecation of prometheus.zeek (Christian Kreibich, Corelight) diff --git a/VERSION b/VERSION index e0a1592835..cc218b636a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -7.1.0-dev.37 +7.1.0-dev.40 diff --git a/src/Func.cc b/src/Func.cc index 87783a30a9..85d184295d 100644 --- a/src/Func.cc +++ b/src/Func.cc @@ -434,7 +434,7 @@ ValPtr ScriptFunc::Invoke(zeek::Args* args, Frame* parent) const { // Warn if the function returns something, but we returned from // the function without an explicit return, or without a value. - else if ( GetType()->Yield() && GetType()->Yield()->Tag() != TYPE_VOID && + else if ( GetType()->Yield() && GetType()->Yield()->Tag() != TYPE_VOID && ! GetType()->ExpressionlessReturnOkay() && (flow != FLOW_RETURN /* we fell off the end */ || ! result /* explicit return with no result */) && ! f->HasDelayed() ) reporter->Warning("non-void function returning without a value: %s", Name()); diff --git a/src/script_opt/Expr.cc b/src/script_opt/Expr.cc index 1ad24c5277..353604f1a7 100644 --- a/src/script_opt/Expr.cc +++ b/src/script_opt/Expr.cc @@ -1238,10 +1238,28 @@ ExprPtr EqExpr::Reduce(Reducer* c, StmtPtr& red_stmt) { if ( IsHasElementsTest() ) return BuildHasElementsTest()->Reduce(c, red_stmt); - if ( GetType()->Tag() == TYPE_BOOL && same_singletons(op1, op2) ) { - bool t = Tag() == EXPR_EQ; - auto res = with_location_of(make_intrusive(val_mgr->Bool(t)), this); - return res->Reduce(c, red_stmt); + if ( GetType()->Tag() == TYPE_BOOL ) { + if ( same_singletons(op1, op2) ) { + bool t = Tag() == EXPR_EQ; + auto res = with_location_of(make_intrusive(val_mgr->Bool(t)), this); + return res->Reduce(c, red_stmt); + } + + if ( op1->GetType()->Tag() == TYPE_BOOL ) { + if ( op1->Tag() == EXPR_CONST ) + std::swap(op1, op2); + + if ( op2->Tag() == EXPR_CONST ) { + bool t = Tag() == EXPR_EQ; + if ( op2->AsConstExpr()->Value()->IsZero() ) + t = ! t; + if ( t ) + return op1->Reduce(c, red_stmt); + + auto res = with_location_of(make_intrusive(op1), this); + return res->Reduce(c, red_stmt); + } + } } return BinaryExpr::Reduce(c, red_stmt); diff --git a/src/script_opt/Stmt.cc b/src/script_opt/Stmt.cc index 618d52ebda..8ef72c60b4 100644 --- a/src/script_opt/Stmt.cc +++ b/src/script_opt/Stmt.cc @@ -245,6 +245,12 @@ StmtPtr IfStmt::DoReduce(Reducer* c) { red_e_stmt = cond_red_stmt; } + // Check again for negation given above reductions/replacements. + if ( e->Tag() == EXPR_NOT ) { + std::swap(s1, s2); + e = e->GetOp1(); + } + StmtPtr sl; if ( e->IsConst() ) { diff --git a/testing/btest/Baseline/language.when/out b/testing/btest/Baseline/language.when/out index 07f8c832ca..5e61f7ed4b 100644 --- a/testing/btest/Baseline/language.when/out +++ b/testing/btest/Baseline/language.when/out @@ -1,3 +1,4 @@ ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. done lookup successful +received termination signal diff --git a/testing/btest/language/when.zeek b/testing/btest/language/when.zeek index d0d07b44fa..e32785abc7 100644 --- a/testing/btest/language/when.zeek +++ b/testing/btest/language/when.zeek @@ -1,6 +1,6 @@ # @TEST-EXEC: btest-bg-run test1 zeek -b %INPUT # @TEST-EXEC: btest-bg-wait 10 -# @TEST-EXEC: mv test1/.stdout out +# @TEST-EXEC: cat test1/.stdout test1/.stderr >> out # @TEST-EXEC: btest-diff out redef exit_only_after_terminate = T; @@ -26,6 +26,13 @@ event zeek_init() when [h] ( local hname3 = lookup_addr(h) ) {} timeout to + 2sec {} + # The following used to generate a spurious warning, so it's here + # as a regression test. + when ( local res = lookup_addr(127.0.0.1) ) + { + return; + } + print "done"; }