[core] Bound StringRef's JSON conversion by the view length

This commit is contained in:
J. Nick Koston
2026-09-08 04:15:24 +02:00
parent 82d399400e
commit e715696bd7
+8 -1
View File
@@ -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