Merge remote-tracking branch 'origin/topic/awelzel/fix-assert-cond-twice'

* origin/topic/awelzel/fix-assert-cond-twice:
  Stmt: Fix assert evaluating cond twice

(cherry picked from commit 6867eda621)
This commit is contained in:
Arne Welzel 2024-03-04 18:22:34 +01:00 committed by Tim Wojtulewicz
parent 1d381b264b
commit 6b6942f9b2
4 changed files with 36 additions and 1 deletions

View file

@ -1644,7 +1644,7 @@ ValPtr AssertStmt::Exec(Frame* f, StmtFlowType& flow) {
bool run_result_hook = assertion_result_hook && assertion_result_hook->HasEnabledBodies(); bool run_result_hook = assertion_result_hook && assertion_result_hook->HasEnabledBodies();
auto assert_result = cond->Eval(f)->AsBool(); auto assert_result = cond->Eval(f)->AsBool();
if ( ! cond->Eval(f)->AsBool() || run_result_hook ) { if ( ! assert_result || run_result_hook ) {
zeek::StringValPtr msg_val = zeek::val_mgr->EmptyString(); zeek::StringValPtr msg_val = zeek::val_mgr->EmptyString();
if ( msg ) { if ( msg ) {

View file

@ -0,0 +1 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.

View file

@ -0,0 +1,6 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
zeek_init
returning true
assertion_result, T, always_true(), always true, <...>/assert-hook.zeek, 23
returning false
assertion_result, F, always_false(), always false, <...>/assert-hook.zeek, 24

View file

@ -196,3 +196,31 @@ event zeek_done()
assert 2 + 2 == 5, "this is false"; assert 2 + 2 == 5, "this is false";
print "not reached"; print "not reached";
} }
@TEST-START-NEXT
# Ensure cond is only evaluated once.
hook assertion_result(result: bool, cond: string, msg: string, bt: Backtrace)
{
print "assertion_result", result, cond, msg, bt[0]$file_location, bt[0]$line_location;
break;
}
function always_true(): bool
{
print "returning true";
return T;
}
function always_false(): bool
{
print "returning false";
return F;
}
event zeek_init()
{
print "zeek_init";
assert always_true(), "always true";
assert always_false(), "always false";
print "not reached";
}