EventMgr: Do not cache current event attributes

Avoid proliferation of various members on EventMgr by storing the
pointer of the current event instead.

This subtly changes the behavior of some builtin functions as they would
have returned the prior event's data when executed outside of event
draining (e.g. C++ level hook invocations), but I think that's actually
for the better.
This commit is contained in:
Arne Welzel 2025-03-27 11:10:08 +01:00
parent bef923ebeb
commit 7dadbb0c1d
4 changed files with 30 additions and 39 deletions

View file

@ -64,14 +64,6 @@ void Event::Dispatch(bool no_remote) {
reporter->EndErrorHandler();
}
EventMgr::EventMgr() {
head = tail = nullptr;
current_src = util::detail::SOURCE_LOCAL;
current_aid = 0;
current_ts = 0;
draining = false;
}
EventMgr::~EventMgr() {
while ( head ) {
Event* n = head->NextEvent();
@ -103,10 +95,10 @@ void EventMgr::QueueEvent(Event* event) {
}
void EventMgr::Dispatch(Event* event, bool no_remote) {
current_src = event->Source();
current_aid = event->Analyzer();
current_ts = event->Time();
Event* old_current = current;
current = event;
event->Dispatch(no_remote);
current = old_current;
Unref(event);
}
@ -116,8 +108,6 @@ void EventMgr::Drain() {
PLUGIN_HOOK_VOID(HOOK_DRAIN_EVENTS, HookDrainEvents());
draining = true;
// Past Zeek versions drained as long as there events, including when
// a handler queued new events during its execution. This could lead
// to endless loops in case a handler kept triggering its own event.
@ -126,27 +116,25 @@ void EventMgr::Drain() {
// that expect the old behavior to trigger something quickly.
for ( int round = 0; head && round < 2; round++ ) {
Event* current = head;
Event* event = head;
head = nullptr;
tail = nullptr;
while ( current ) {
Event* next = current->NextEvent();
while ( event ) {
Event* next = event->NextEvent();
current_src = current->Source();
current_aid = current->Analyzer();
current_ts = current->Time();
current->Dispatch();
Unref(current);
current = event;
event->Dispatch();
Unref(event);
++event_mgr.num_events_dispatched;
current = next;
event = next;
}
}
// Note: we might eventually need a general way to specify things to
// do after draining events.
draining = false;
current = nullptr;
// Make sure all of the triggers get processed every time the events
// drain.