From d2b7d40716476f72148d057495c1e402fedd8696 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 19 Feb 2026 12:17:19 -0600 Subject: [PATCH] Clamp accuracy_decimals to -9 to prevent uint32_t overflow --- esphome/core/helpers.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index dc93ff331e..be2cb1b3f0 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -468,14 +468,16 @@ ParseOnOffState parse_on_off(const char *str, const char *on, const char *off) { static inline void normalize_accuracy_decimals(float &value, int8_t &accuracy_decimals) { if (accuracy_decimals < 0) { + // Clamp to -9 to keep divisor within uint32_t range (max 10^9 = 1,000,000,000) + int8_t dec = accuracy_decimals < -9 ? (int8_t) -9 : accuracy_decimals; uint32_t divisor; - if (accuracy_decimals == -1) { + if (dec == -1) { divisor = 10; - } else if (accuracy_decimals == -2) { + } else if (dec == -2) { divisor = 100; } else { divisor = 1000; - for (int8_t i = accuracy_decimals + 3; i < 0; i++) + for (int8_t i = dec + 3; i < 0; i++) divisor *= 10; } auto divisor_f = static_cast(divisor);