diff --git a/CHANGELOG.md b/CHANGELOG.md index fbd379c8d27..1d1e03b6923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -228,6 +228,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())