When a received MQTT message is read in multiple chunks under esp32 MQTT backend, any messages after the first will have a nullptr topic pointer. This then causes a std::logic_error to be raised when creating a std::string for the topic in mqtt_client, leading to a panic and reboot.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
The problem
When a received MQTT message is read in multiple chunks under esp32 MQTT backend, any messages after the first will have a nullptr topic pointer. This then causes a std::logic_error to be raised when creating a std::string for the topic in mqtt_client, leading to a panic and reboot.
Topic is null on subsequent chunks from here: https://github.com/espressif/esp-mqtt/blob/7894dd0ace9ef8b035e3686d2767324540c1cfea/mqtt_client.c#L1090 Nullptr gets propagated here due to empty topic: https://github.com/esphome/esphome/blame/32e3f2623973f45bd37b6b0dc956adcd82a45e77/esphome/components/mqtt/mqtt_backend_esp32.cpp#L146 Nullptr causes std::logic_error here in std::string constructor (implicit cast): https://github.com/esphome/esphome/blob/32e3f2623973f45bd37b6b0dc956adcd82a45e77/esphome/components/mqtt/mqtt_client.cpp#L48
The topic needs to be stored from the first segment and then used once all segments have been read. Will send a PR shortly.
Which version of ESPHome has the issue?
2023.9.1 but still present in latest
What type of installation are you using?
Docker
Which version of Home Assistant has the issue?
N/A
What platform are you using?
ESP32-IDF
Board
esp32dev
Component causing the issue
mqtt
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information
No response