From 1ba1ab7fe29dd3d3f6a7be05e05d101ffa2058d5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 18 Feb 2026 17:53:12 -0600 Subject: [PATCH] [json] Document heap fallback buffer sizing expectations Payloads exceeding 1024 bytes are not known to exist in real configurations. One doubling iteration covers all known entity types. Cap at 4096 as a safety limit. --- esphome/components/json/json_util.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/json/json_util.cpp b/esphome/components/json/json_util.cpp index 7a898c17f8..53a9631d6d 100644 --- a/esphome/components/json/json_util.cpp +++ b/esphome/components/json/json_util.cpp @@ -120,7 +120,9 @@ SerializationBuffer<> JsonBuilder::serialize() { } // Payload exceeded stack buffer. Double the buffer and retry until it fits. - // Cap at 4096 to prevent runaway allocation on memory-constrained devices. + // 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. size_t heap_size = buf_size * 2; while (heap_size <= 4096) { result.reallocate_heap_(heap_size - 1);