From 5c7007ebefd917110f4a9e5e28014d31cb2a528b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Mon, 7 Sep 2020 16:55:47 -0700 Subject: [PATCH] ircd::m::vm::eval: Add coarse limit to options for array evals. --- include/ircd/m/vm.h | 4 ++++ matrix/vm_eval.cc | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/ircd/m/vm.h b/include/ircd/m/vm.h index 80d0daefd..2ef6e55d8 100644 --- a/include/ircd/m/vm.h +++ b/include/ircd/m/vm.h @@ -328,6 +328,10 @@ struct ircd::m::vm::opts /// reason to ever adjust this. size_t reserve_index {1024}; + /// Coarse limit for array evals. The counter is incremented for every + /// event; both accepted and faulted. + size_t limit = -1; + /// Mask of faults that are not thrown as exceptions out of eval(). If /// masked, the fault is returned from eval(). By default, the EXISTS /// fault is masked which means existing events won't kill eval loops. diff --git a/matrix/vm_eval.cc b/matrix/vm_eval.cc index 1075bf436..e6969b440 100644 --- a/matrix/vm_eval.cc +++ b/matrix/vm_eval.cc @@ -370,6 +370,8 @@ ircd::m::vm::eval::eval(const json::array &pdus, :eval{opts} { std::vector events(begin(pdus), end(pdus)); + if(events.size() > opts.limit) + events.resize(opts.limit); // Sort the events first to avoid complicating the evals; the events might // be from different rooms but it doesn't matter. @@ -411,8 +413,8 @@ ircd::m::vm::eval::operator()(const vector_view &events) // Conduct each eval without letting any one exception ruin things for the // others, including an interrupt. The only exception is a termination. - size_t ret(0); - for(auto it(begin(events)); it != end(events); ++it) try + size_t ret(0), i(0); + for(auto it(begin(events)); it != end(events) && i < opts->limit; ++it, ++i) try { const m::event &event {