simplified "assert" by not trying to catch messages that themselves have errors

This commit is contained in:
Vern Paxson 2024-12-02 10:37:10 -08:00
parent a2a47ba334
commit 05e913db1b
6 changed files with 17 additions and 31 deletions

View file

@ -1597,23 +1597,11 @@ ValPtr AssertStmt::Exec(Frame* f, StmtFlowType& flow) {
if ( ! assert_result || 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 )
// Eval() may fail if expression assumes assert // It's up to the script writing to assure that the expression
// condition is F, but we still try to get it for // works regardless of the state of the condition. If they
// the assertion_result hook. // fail to do so, they can get an exception at this point.
try {
msg_val = cast_intrusive<zeek::StringVal>(msg->Eval(f)); msg_val = cast_intrusive<zeek::StringVal>(msg->Eval(f));
} catch ( InterpreterException& e ) {
static ODesc desc;
desc.Clear();
desc.SetShort(true);
desc.SetQuotes(true);
desc.Add("<error eval ");
msg->Describe(&desc);
desc.Add(">");
msg_val = zeek::make_intrusive<zeek::StringVal>(desc.Len(), (const char*)desc.Bytes());
}
}
report_assert(assert_result, cond_desc, msg_val, GetLocationInfo()); report_assert(assert_result, cond_desc, msg_val, GetLocationInfo());
} }

View file

@ -1,4 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
expression error in <...>/assert.zeek, line 10: field value missing (r$b) expression error in <...>/assert.zeek, line 12: field value missing (r$b)
error in <...>/assert.zeek, line 10: assertion failure: r?$b (<error eval fmt("r$b is not set trying anyway: %s", r$b)>)
fatal error: errors occurred while initializing fatal error: errors occurred while initializing

View file

@ -1,5 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
expression error in <...>/assert-hook.zeek, line 15: field value missing (get_current_packet_header()$ip) expression error in <...>/assert-hook.zeek, line 18: field value missing (get_current_packet_header()$ip)
expression error in <...>/assert-hook.zeek, line 17: field value missing (get_current_packet_header()$ip) error in <...>/assert-hook.zeek, line 24: assertion failure: 2 + 2 == 5 ({"msg":"false and works"})
error in <...>/assert-hook.zeek, line 17: assertion failure: 2 + 2 == 5 (<error eval cat(get_current_packet_header()$ip)>)
error in <...>/assert-hook.zeek, line 22: assertion failure: 2 + 2 == 5 ({"msg":"false and works"})

View file

@ -1,7 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
assertion_result, T, 2 + 2 == 4, <error eval cat(get_current_packet_header()$ip)>, <...>/assert-hook.zeek, 15 assertion_result, T, 2 + 2 == 4, {"msg":"true and works"}, <...>/assert-hook.zeek, 15
assertion_result, T, 2 + 2 == 4, {"msg":"true and works"}, <...>/assert-hook.zeek, 16 assertion_result, F, 2 + 2 == 5, {"msg":"false and works"}, <...>/assert-hook.zeek, 24
assertion_result, F, 2 + 2 == 5, <error eval cat(get_current_packet_header()$ip)>, <...>/assert-hook.zeek, 17 assertion_failure, 2 + 2 == 5, {"msg":"false and works"}, <...>/assert-hook.zeek, 24
assertion_failure, 2 + 2 == 5, <error eval cat(get_current_packet_header()$ip)>, <...>/assert-hook.zeek, 17
assertion_result, F, 2 + 2 == 5, {"msg":"false and works"}, <...>/assert-hook.zeek, 22
assertion_failure, 2 + 2 == 5, {"msg":"false and works"}, <...>/assert-hook.zeek, 22

View file

@ -164,9 +164,11 @@ hook assertion_result(result: bool, cond: string, msg: string, bt: Backtrace)
event zeek_init() event zeek_init()
{ {
assert 2 + 2 == 4, cat(get_current_packet_header()$ip);
assert 2 + 2 == 4, to_json([$msg="true and works"]); assert 2 + 2 == 4, to_json([$msg="true and works"]);
assert 2 + 2 == 5, cat(get_current_packet_header()$ip); # This next assert will generate a run-time error, exiting the
# event handler.
assert 2 + 2 == 4, cat(get_current_packet_header()$ip);
assert 2 + 2 == 5, "didn't get to here";
} }
event zeek_done() event zeek_done()

View file

@ -69,6 +69,8 @@ event zeek_init()
{ {
local r: MyRecord = [$a=1234]; local r: MyRecord = [$a=1234];
assert ! r?$b, fmt("Unexpected r$b is set to %s", r$b); assert ! r?$b, fmt("Unexpected r$b is set to %s", r$b);
# This will generate a run-time error, rather than reporting the
# failed assertion.
assert r?$b, fmt("r$b is not set trying anyway: %s", r$b); assert r?$b, fmt("r$b is not set trying anyway: %s", r$b);
} }