From e960c29acb9d81da69ced871ab5054c3e2152d3b Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Tue, 23 Jul 2024 15:18:21 -0700 Subject: [PATCH 1/2] fix & regression test for GH-3839 (spurious warnings for "when" constructs) --- src/Func.cc | 2 +- testing/btest/language/when.zeek | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) 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/testing/btest/language/when.zeek b/testing/btest/language/when.zeek index d0d07b44fa..d8a447e8fd 100644 --- a/testing/btest/language/when.zeek +++ b/testing/btest/language/when.zeek @@ -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"; } From ff7466df6e54a548fa58303993cad8fcf9728a1b Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Tue, 23 Jul 2024 15:31:59 -0700 Subject: [PATCH 2/2] minor optimization of boolean comparisons --- src/script_opt/Expr.cc | 26 ++++++++++++++++++++++---- src/script_opt/Stmt.cc | 6 ++++++ 2 files changed, 28 insertions(+), 4 deletions(-) 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() ) {