diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index bc209a7d11..8d04d376f6 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -27,21 +27,20 @@ inline char *append_char(char *p, char c) { // Max lengths for stack-based topic building. // These limits are enforced at Python config validation time in mqtt/__init__.py // using cv.Length() validators for topic_prefix and discovery_prefix. +// MQTT_COMPONENT_TYPE_MAX_LEN and MQTT_SUFFIX_MAX_LEN are defined in mqtt_component.h. // OBJECT_ID_MAX_LEN is defined in entity_base.h and derived from friendly_name limits. // This ensures the stack buffers below are always large enough. static constexpr size_t TOPIC_PREFIX_MAX_LEN = 64; // Validated in Python: cv.Length(max=64) -static constexpr size_t COMPONENT_TYPE_MAX_LEN = 20; // Longest: "alarm_control_panel" = 19 -static constexpr size_t SUFFIX_MAX_LEN = 32; // Longest: "target_temperature_high/command" = 28 -static constexpr size_t SANITIZED_NAME_MAX_LEN = 64; // Same as topic_prefix (device name) +static constexpr size_t SANITIZED_NAME_MAX_LEN = 64; // Safe: hostname validation limits to 31 chars static constexpr size_t DISCOVERY_PREFIX_MAX_LEN = 64; // Validated in Python: cv.Length(max=64) // Stack buffer sizes - safe because all inputs are length-validated at config time // Format: prefix + "/" + type + "/" + object_id + "/" + suffix + null static constexpr size_t DEFAULT_TOPIC_MAX_LEN = - TOPIC_PREFIX_MAX_LEN + 1 + COMPONENT_TYPE_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 1 + SUFFIX_MAX_LEN + 1; + TOPIC_PREFIX_MAX_LEN + 1 + MQTT_COMPONENT_TYPE_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 1 + MQTT_SUFFIX_MAX_LEN + 1; // Format: prefix + "/" + type + "/" + name + "/" + object_id + "/config" + null -static constexpr size_t DISCOVERY_TOPIC_MAX_LEN = - DISCOVERY_PREFIX_MAX_LEN + 1 + COMPONENT_TYPE_MAX_LEN + 1 + SANITIZED_NAME_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 7 + 1; +static constexpr size_t DISCOVERY_TOPIC_MAX_LEN = DISCOVERY_PREFIX_MAX_LEN + 1 + MQTT_COMPONENT_TYPE_MAX_LEN + 1 + + SANITIZED_NAME_MAX_LEN + 1 + OBJECT_ID_MAX_LEN + 7 + 1; void MQTTComponent::set_qos(uint8_t qos) { this->qos_ = qos; }