From 05dbc0035bdf46a56eb065367157a2cc1c9222cf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Jan 2026 11:03:04 -1000 Subject: [PATCH] handle conversion failure --- esphome/core/string_ref.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/esphome/core/string_ref.h b/esphome/core/string_ref.h index e13b752e00..52ddbb8133 100644 --- a/esphome/core/string_ref.h +++ b/esphome/core/string_ref.h @@ -194,15 +194,17 @@ namespace internal { template inline R parse_number(const StringRef &str, size_t *pos, F conv) { char *end; R result = conv(str.c_str(), &end); + // Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number if (pos) - *pos = static_cast(end - str.c_str()); + *pos = (end == str.c_str()) ? 0 : static_cast(end - str.c_str()); return result; } template inline R parse_number(const StringRef &str, size_t *pos, int base, F conv) { char *end; R result = conv(str.c_str(), &end, base); + // Set pos to 0 on conversion failure (when no characters consumed), otherwise index after number if (pos) - *pos = static_cast(end - str.c_str()); + *pos = (end == str.c_str()) ? 0 : static_cast(end - str.c_str()); return result; } // NOLINTEND(google-runtime-int)