From bb6600985ffaa1ee7298bf53e1c89bb4fe7622ff Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 18 Feb 2026 17:53:29 -0600 Subject: [PATCH] [json] Make heap fallback max size constexpr --- esphome/components/json/json_util.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esphome/components/json/json_util.cpp b/esphome/components/json/json_util.cpp index 53a9631d6d..8734d2e1e7 100644 --- a/esphome/components/json/json_util.cpp +++ b/esphome/components/json/json_util.cpp @@ -123,8 +123,9 @@ SerializationBuffer<> JsonBuilder::serialize() { // In practice, one iteration (1024 bytes) covers all known entity types. // Payloads exceeding 1024 bytes are not known to exist in real configurations. // Cap at 4096 as a safety limit to prevent runaway allocation. + constexpr size_t max_heap_size = 4096; size_t heap_size = buf_size * 2; - while (heap_size <= 4096) { + while (heap_size <= max_heap_size) { result.reallocate_heap_(heap_size - 1); size = serializeJson(doc_, result.data_writable_(), heap_size); if (size < heap_size) {