From 97881ac976c719dc5083eb8e83e8ed923759d647 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 13 Sep 2019 15:27:02 -0700 Subject: [PATCH] ircd::m::vm::eval: Fix generation and scope duration of event_id in array eval. --- ircd/m.cc | 23 +++++++++++++++++------ modules/m_vm.cc | 5 +++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ircd/m.cc b/ircd/m.cc index 102cb6500..ca302a6d1 100644 --- a/ircd/m.cc +++ b/ircd/m.cc @@ -1390,17 +1390,28 @@ ircd::m::vm::eval::eval(const json::array &pdus, for(auto it(begin(events)); it != end(events); ++it) try { auto &event{*it}; - if(!json::get<"event_id"_>(event)) - event.event_id = event::id::v4 - { - this->event_id, event - }; + + // If we set the event_id in the event instance we have to unset + // it so other contexts don't see an invalid reference. + const unwind event_id{[&event] + { + event.event_id = json::get<"event_id"_>(event)? + event.event_id: + m::event::id{}; + }}; + + // We have to set the event_id in the event instance if it didn't come + // with the event JSON. + if(!opts.edu && !event.event_id) + event.event_id = opts.room_version == "3"? + event::id{event::id::v3{this->event_id, event}}: + event::id{event::id::v4{this->event_id, event}}; // When a fault::EXISTS would not actually be revealed to the user in // any way we can elide a lot of grief by checking this here first and // skipping the event. The query path will be adequately cached anyway. if(~(opts.warnlog | opts.errorlog) & fault::EXISTS) - if(m::exists(event.event_id)) + if(event.event_id && m::exists(event.event_id)) continue; operator()(event); diff --git a/modules/m_vm.cc b/modules/m_vm.cc index 1d4c46b60..e1130616b 100644 --- a/modules/m_vm.cc +++ b/modules/m_vm.cc @@ -692,6 +692,11 @@ try eval.event_, &event }; + const scope_restore eval_event_id + { + eval.event_id, event.event_id? event.event_id : eval.event_id + }; + // Set a member to the room_id for convenient access, without stepping on // any room_id reference that exists there for whatever reason. const scope_restore eval_room_id