eclipse-paho / paho.mqtt.python

paho.mqtt.python
Other
2.21k stars 721 forks source link

There is an extra Byte in the payload if the properties packet length is greater or equal to 128 #541

Closed ochabloz closed 3 years ago

ochabloz commented 3 years ago

There is an extra byte in the payload if the length of the property section of a publish packet is greater or equal to 128

How to reproduce the error:

import threading
from src.paho.mqtt import client as mqtt
from src.paho.mqtt import packettypes

url, port, user, pw = "test.mosquitto.org", 1883, "", ""
ref_payload = b"abcdefghij"
topic = "test/corruption"
emitter = mqtt.Client(protocol=mqtt.MQTTv5)
emitter.connect(url, port, 120, clean_start=True)
emitter.loop_start()
recv = mqtt.Client(protocol=mqtt.MQTTv5)
recv.connect(url, port, 120, clean_start=True)
recv.loop_start()
recv.subscribe(topic, qos=2)
event = threading.Event()
payload = []

def on_message(c, u, m):
    payload.append(m.payload)
    event.set()

recv.on_message = on_message
props = mqtt.Properties(packettypes.PacketTypes.PUBLISH)
props.CorrelationData = b"\x00\x00\x00\x01"
props.ResponseTopic = "test_bimbibmm/JB007/measurement"
props.UserProperty = ("serial_number", "TST")
props.UserProperty = ("product_number", "TST")
props.UserProperty = ("vrd", "261DAEE5")
props.UserProperty = ("timestamp", "1234567.123400")
emitter.publish(topic, ref_payload, qos=0, properties=props)

event.wait()
p = payload.pop()
assert ref_payload == p

With the message above, the property section is EXACTLY 128 bytes long. If you remove 1 character from any user property, (p.ex props.UserProperty = ("timestamp", "1234567.12340")) the assertion will then be correct

ralight commented 3 years ago

Thanks very much, that should be fixed now.

ochabloz commented 3 years ago

Hello @ralight, thanks for your solution and for your reactivity.