mirror of
https://github.com/esphome/esphome.git
synced 2026-09-01 18:46:02 +00:00
ArduinoJson's serializeJson() with a bounded buffer returns the actual bytes written (truncated count), NOT the would-be size like snprintf(). When the payload exceeded the 512-byte stack buffer, the return value was used as the exact size for heap reallocation, but this was only the truncated count. The heap buffer was allocated too small, the second serialization was also truncated, and no NUL terminator was written. This caused corrupted JSON in SSE streams for entities with payloads exceeding 512 bytes (e.g., climate DETAIL_ALL with many features). Fix: use measureJson() in the heap fallback path to get the exact untruncated size before allocating. Also add the missing set_size_() call after the second serialization to ensure proper NUL termination.