From 5f8c97453f2698bd1d724840bb1a3c71213c0319 Mon Sep 17 00:00:00 2001 From: Ole Petter Date: Wed, 1 Nov 2023 13:40:09 +0100 Subject: [PATCH] chore(state-machine): Panic in the case of non-handled non-deferred events This adds a sanity check to verify that there are no non-deferred events left in the event queue after the run_queue has been populated. If there was, then we would have undefined behaviour in the state-machine, and as such the best thing we can do in this instance is to panic. This is a serious logic error in our code, and as such such cause a hard-fault. Signed-off-by: Ole Petter --- src/common/state_machine.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/common/state_machine.hpp b/src/common/state_machine.hpp index 0fa4a3ff7..53a135fbb 100644 --- a/src/common/state_machine.hpp +++ b/src/common/state_machine.hpp @@ -215,9 +215,27 @@ class StateMachineRunner : virtual public EventPoster { return run_queue; } + + void FailIfNonDeferredEventsLeftInEventQueue(queue queue_copy) { + // Check if there are any non-deferred events in the queue - then fail if + while (not queue_copy.empty()) { + EventType event = queue_copy.front(); + queue_copy.pop(); + for (const auto machine : machines_) { + if (machine->deferred_events_.find(event) == machine->deferred_events_.end()) { + log::Fatal( + "The state machine has an unprocessed non-deferred event in the queue. This is a programming error!"); + } + } + } + } + + void RunOne() { vector *> run_queue = FillRunQueueFrom(event_queue_); + FailIfNonDeferredEventsLeftInEventQueue(event_queue_); + if (!run_queue.empty()) { for (auto &state : run_queue) { log::Trace("Entering state " + common::BestAvailableTypeName(*state));