From 4b22d9f04b67383f7ee800f273b410f62621cbd5 Mon Sep 17 00:00:00 2001 From: "julian.speith" Date: Wed, 18 Nov 2026 12:34:06 +0100 Subject: [PATCH] Reject a nullptr in the FSM solver's state register and transition logic solve_fsm checked that the state register and the transition logic are not empty, but not what they contain. A nullptr in the state register, e.g. a None passed from Python, was dereferenced while building the next-state functions and crashed HAL. A nullptr in the transition logic did not crash, but was only caught four levels down, by the subgraph function cache, with an error that did not say where it came from. Check both lists up front and name the index of the nullptr in the error, as is already done for the nets of an output. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + plugins/solve_fsm/src/solve_fsm.cpp | 16 ++++++++++++++++ plugins/solve_fsm/test/solve_fsm.cpp | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 937b3f39ebd..709efab7946 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -205,6 +205,7 @@ All notable changes to this project will be documented in this file. * added `to_string` and `write_txt` to the state transition graph, printing the full conditions of all transitions together with a legend that maps the state bits, the outputs, and every net variable of a Boolean function back to the netlist * added tests * removed the debug output that `solve_fsm` printed to stdout on every run + * fixed `solve_fsm` crashing on a nullptr in the state register, which is now rejected with an error naming its index, as is a nullptr in the transition logic * fixed `solve_fsm` interpreting a user-provided initial state with the wrong bit order * netlist preprocessing * added an optional gate scope to the preprocessing functions, restricting which gates may be modified or deleted and defaulting to the entire netlist diff --git a/plugins/solve_fsm/src/solve_fsm.cpp b/plugins/solve_fsm/src/solve_fsm.cpp index 847546d0fcb..04e0e1083d1 100644 --- a/plugins/solve_fsm/src/solve_fsm.cpp +++ b/plugins/solve_fsm/src/solve_fsm.cpp @@ -636,6 +636,22 @@ namespace hal return ERR("failed to solve FSM: no transition logic configured."); } + for (u32 i = 0; i < config.state_register.size(); i++) + { + if (config.state_register.at(i) == nullptr) + { + return ERR("failed to solve FSM: the flip-flop at index " + std::to_string(i) + " of the state register is a nullptr."); + } + } + + for (u32 i = 0; i < config.transition_logic.size(); i++) + { + if (config.transition_logic.at(i) == nullptr) + { + return ERR("failed to solve FSM: the gate at index " + std::to_string(i) + " of the transition logic is a nullptr."); + } + } + const u32 state_size = config.state_register.size(); if (state_size > 64) { diff --git a/plugins/solve_fsm/test/solve_fsm.cpp b/plugins/solve_fsm/test/solve_fsm.cpp index 3399bf80381..88c996f58d3 100644 --- a/plugins/solve_fsm/test/solve_fsm.cpp +++ b/plugins/solve_fsm/test/solve_fsm.cpp @@ -470,6 +470,17 @@ namespace hal EXPECT_TRUE(solve_fsm::solve_fsm(solve_fsm::Configuration(nl.get()).with_transition_logic(transition_logic)).is_error()); EXPECT_TRUE(solve_fsm::solve_fsm(solve_fsm::Configuration(nl.get()).with_state_register(sr.gates())).is_error()); + // a nullptr in the state register or the transition logic is rejected instead of dereferenced, and the error names where it is + const auto expect_nullptr_error = [&](const std::vector& state_register, const std::vector& logic, const std::string& expected) { + const auto res = solve_fsm::solve_fsm(solve_fsm::Configuration(nl.get()).with_state_register(state_register).with_transition_logic(logic)); + ASSERT_TRUE(res.is_error()); + EXPECT_NE(res.get_error().get().find(expected), std::string::npos) << res.get_error().get(); + }; + expect_nullptr_error({nullptr, sr.ff0, sr.ff1}, transition_logic, "flip-flop at index 0 of the state register is a nullptr"); + expect_nullptr_error({sr.ff0, sr.ff1, nullptr}, transition_logic, "flip-flop at index 2 of the state register is a nullptr"); + expect_nullptr_error(sr.gates(), {nullptr, transition_logic.at(0), transition_logic.at(1)}, "gate at index 0 of the transition logic is a nullptr"); + expect_nullptr_error(sr.gates(), {transition_logic.at(0), transition_logic.at(1), nullptr}, "gate at index 2 of the transition logic is a nullptr"); + // an initial state that does not cover the whole state register is rejected as well const auto incomplete = solve_fsm::Configuration(nl.get()) .with_state_register(sr.gates())