mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
simplified "assert" by not trying to catch messages that themselves have errors
This commit is contained in:
parent
a2a47ba334
commit
05e913db1b
6 changed files with 17 additions and 31 deletions
20
src/Stmt.cc
20
src/Stmt.cc
|
@ -1597,23 +1597,11 @@ ValPtr AssertStmt::Exec(Frame* f, StmtFlowType& flow) {
|
|||
if ( ! assert_result || run_result_hook ) {
|
||||
zeek::StringValPtr msg_val = zeek::val_mgr->EmptyString();
|
||||
|
||||
if ( msg ) {
|
||||
// Eval() may fail if expression assumes assert
|
||||
// condition is F, but we still try to get it for
|
||||
// the assertion_result hook.
|
||||
try {
|
||||
if ( msg )
|
||||
// It's up to the script writing to assure that the expression
|
||||
// works regardless of the state of the condition. If they
|
||||
// fail to do so, they can get an exception at this point.
|
||||
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());
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
### 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)
|
||||
error in <...>/assert.zeek, line 10: assertion failure: r?$b (<error eval fmt("r$b is not set trying anyway: %s", r$b)>)
|
||||
expression error in <...>/assert.zeek, line 12: field value missing (r$b)
|
||||
fatal error: errors occurred while initializing
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
### 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 17: field value missing (get_current_packet_header()$ip)
|
||||
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"})
|
||||
expression error in <...>/assert-hook.zeek, line 18: field value missing (get_current_packet_header()$ip)
|
||||
error in <...>/assert-hook.zeek, line 24: assertion failure: 2 + 2 == 5 ({"msg":"false and works"})
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
### 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, 16
|
||||
assertion_result, F, 2 + 2 == 5, <error eval cat(get_current_packet_header()$ip)>, <...>/assert-hook.zeek, 17
|
||||
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
|
||||
assertion_result, T, 2 + 2 == 4, {"msg":"true and works"}, <...>/assert-hook.zeek, 15
|
||||
assertion_result, F, 2 + 2 == 5, {"msg":"false and works"}, <...>/assert-hook.zeek, 24
|
||||
assertion_failure, 2 + 2 == 5, {"msg":"false and works"}, <...>/assert-hook.zeek, 24
|
||||
|
|
|
@ -164,9 +164,11 @@ hook assertion_result(result: bool, cond: string, msg: string, bt: Backtrace)
|
|||
|
||||
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 == 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()
|
||||
|
|
|
@ -69,6 +69,8 @@ event zeek_init()
|
|||
{
|
||||
local r: MyRecord = [$a=1234];
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue