From a328726bfd8fcaaa1644126ec4e199e62816e14f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 5 Apr 2026 12:08:05 -1000 Subject: [PATCH] Derive exception cause range bounds from table sizes --- esphome/components/esp8266/crash_handler.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esphome/components/esp8266/crash_handler.cpp b/esphome/components/esp8266/crash_handler.cpp index 0bd58cc545..9f9eacf76c 100644 --- a/esphome/components/esp8266/crash_handler.cpp +++ b/esphome/components/esp8266/crash_handler.cpp @@ -163,11 +163,13 @@ PROGMEM_STRING_TABLE(ExcCauseHigh, // Causes 12-29 (offset by 12) ); // clang-format on +static constexpr uint32_t EXC_CAUSE_HIGH_BASE = 12; // First cause code in ExcCauseHigh table + static const LogString *get_exception_cause(uint32_t cause) { - if (cause <= 9) + if (cause < ExcCauseLow::COUNT) return ExcCauseLow::get_log_str(cause, ExcCauseLow::LAST_INDEX); - if (cause >= 12 && cause <= 29) { - const LogString *str = ExcCauseHigh::get_log_str(cause - 12, ExcCauseHigh::LAST_INDEX); + if (cause >= EXC_CAUSE_HIGH_BASE && cause < EXC_CAUSE_HIGH_BASE + ExcCauseHigh::COUNT) { + const LogString *str = ExcCauseHigh::get_log_str(cause - EXC_CAUSE_HIGH_BASE, ExcCauseHigh::LAST_INDEX); // Empty strings are gap entries — return nullptr so caller knows cause is unknown if (LOG_STR_ARG(str)[0] == '\0') return nullptr;