From e715696bd7f6fdca8137c4f8c573fef08223a77b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 8 Sep 2026 04:15:24 +0200 Subject: [PATCH] [core] Bound StringRef's JSON conversion by the view length --- esphome/core/string_ref.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/esphome/core/string_ref.h b/esphome/core/string_ref.h index 0c4a1d3fa8..ca6368d956 100644 --- a/esphome/core/string_ref.h +++ b/esphome/core/string_ref.h @@ -263,7 +263,14 @@ inline double stod(const StringRef &str, size_t *pos = nullptr) { #ifdef USE_JSON // NOLINTNEXTLINE(readability-identifier-naming) -inline void convertToJson(const StringRef &src, JsonVariant dst) { dst.set(src.empty() ? "" : src.c_str()); } +inline void convertToJson(const StringRef &src, JsonVariant dst) { + // Bounded by the view length; a null, empty view becomes "" rather than JSON null + if (src.empty()) { + dst.set(""); + return; + } + dst.set(JsonString(src.c_str(), src.size())); +} #endif // USE_JSON } // namespace esphome