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)