@@ -724,11 +724,12 @@ mqtt_message_t *mqtt5_msg_publish(mqtt_connection_t *connection, const char *top
724724{
725725 init_message (connection );
726726
727- if (topic == NULL || topic [0 ] == '\0' ) {
727+ if ((topic == NULL || topic [0 ] == '\0' ) && (!property || !property -> topic_alias )){
728+ ESP_LOGE (TAG , "Message must have a topic filter or a topic alias set" );
728729 return fail_message (connection );
729730 }
730-
731- APPEND_CHECK (append_property (connection , 0 , 2 , topic , strlen ( topic ) ), fail_message (connection ));
731+ int topic_len = ( topic == NULL || topic [ 0 ] == '\0' ) ? 0 : strlen ( topic );
732+ APPEND_CHECK (append_property (connection , 0 , 2 , topic , topic_len ), fail_message (connection ));
732733
733734 if (data == NULL && data_length > 0 ) {
734735 return fail_message (connection );
@@ -809,18 +810,21 @@ mqtt_message_t *mqtt5_msg_publish(mqtt_connection_t *connection, const char *top
809810int mqtt5_msg_get_reason_code (uint8_t * buffer , size_t length )
810811{
811812 uint8_t len_bytes = 0 ;
812- size_t offset = 1 ;
813- size_t totlen = get_variable_len (buffer , offset , length , & len_bytes );
813+ size_t offset = 1 ; // Message type
814+ size_t variable_len = get_variable_len (buffer , offset , length , & len_bytes );
814815 offset += len_bytes ;
815- totlen += offset ;
816816
817817 switch (mqtt5_get_type (buffer )) {
818818 case MQTT_MSG_TYPE_PUBACK :
819819 case MQTT_MSG_TYPE_PUBREC :
820820 case MQTT_MSG_TYPE_PUBREL :
821821 case MQTT_MSG_TYPE_PUBCOMP :
822+ if (variable_len == 2 ) {
823+ return 0 ;
824+ }
822825 offset += 2 ; //skip the message id
823826 if (offset >= length ) {
827+ ESP_LOGE (TAG , "Invalid control packet, reason code is absent" );
824828 return -1 ;
825829 }
826830 return buffer [offset ];
@@ -833,10 +837,10 @@ int mqtt5_msg_get_reason_code(uint8_t *buffer, size_t length)
833837 size_t property_len = get_variable_len (buffer , offset , length , & len_bytes );
834838 offset = offset + len_bytes + property_len ;
835839 if (offset >= length ) {
840+ ESP_LOGE (TAG , "Invalid control packet, reason code is absent" );
836841 return -1 ;
837- } else {
838- return buffer [offset ];
839842 }
843+ return buffer [offset ];
840844 }
841845 case MQTT_MSG_TYPE_DISCONNECT :
842846 if (offset >= length ) {
0 commit comments