← index #969PR #209
Off-topic · high · value 0.461
QUERY · ISSUE

MQTT subscribe Remaining Length encoding is incorrect

openby peterhinchopened 2025-01-21updated 2025-01-21

See this discussion for background, also MQTT spec V3.1.1 section 2.2.3.

This may be academic as its main impact is if the topic field is long. This is to record the existence of the issue.

The current encoding stores Remaining Length in a byte. It should be stored as a Variable Byte Integer as per .publish. This will cause a failure if Remaining Length is greater than 127, therefore imposing a limit on the length of the topic field.

I can submit a PR if the maintainers think this is worth fixing.

CANDIDATE · PULL REQUEST

Fixed SyntaxError in subscribe()

closedby Himura2laopened 2017-09-21updated 2017-09-26
>>> 1.to_bytes(1)
Traceback (most recent call last):
  File "<stdin>", line 1
SyntaxError: invalid syntax for number
9 comments
pfalcon · 2017-09-21

So, what's exactly being fixed here?

Himura2la · 2017-09-22

I had an exception on this string, because int() has no to_bytes() method. I call subscribe() without QoS and it makes QoS become 0.

pfalcon · 2017-09-22

because int() has no to_bytes() method.

Of course it has:

MicroPython v1.9.2-96-g3c7f2184b-dirty on 2017-09-19; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> (1).to_bytes(1, "little")
b'\x01'
Himura2la · 2017-09-22

You convert a tuple to bytes, but qos is int

https://github.com/Himura2la/micropython-lib/blob/master/umqtt.simple/umqtt/simple.py#L144

>>> (1).to_bytes(1, "little")
b'\x01'
>>> 1.to_bytes(1, "little")
  File "<stdin>", line 1
    1.to_bytes(1, "little")
             ^
SyntaxError: invalid syntax
>>>

Maybe my next commit will be a better option if tuples are OK with to_bytes()

Himura2la · 2017-09-25

So do you see the issue now?

pfalcon · 2017-09-26

I don't, sorry. As far as I can tell, the issue you had was because you used a wrong branch of the project. That branch was rebased since, but why did you use it in the first place?

Himura2la · 2017-09-26

I tested this and it somehow works

MicroPython v1.9.1-8-g7213e78d on 2017-06-12; ESP module with ESP8266   
Type "help()" for more information. 
>>>   
>>> 1.to_bytes(1, "little")   
Traceback (most recent call last):  
  File "<stdin>", line 1   
SyntaxError: invalid syntax for number 
>>> 0.to_bytes(1, "little")   
Traceback (most recent call last):  
  File "<stdin>", line 1   
SyntaxError: invalid syntax for number 
>>> qos=0   
>>> qos.to_bytes(1, "little") 
b'\x00'  
>>> qos=1   
>>> qos.to_bytes(1, "little") 
b'\x01'  
>>> 

Very strange behavior, but OK, it works...

dpgeorge · 2017-09-26

@Himura2la the problem was simply that 1.to_bytes(...) looks like a decimal number to Python and so that's why it gives a syntax error. It can be fixed by putting a space before the period: 1 .to_bytes(...), or as shown above putting the number in parenthesis: (1).to_bytes(...). Note that it's not a tuple with one element, that would be (1,).to_bytes(...) which doesn't work (tuple doesn't have a to_bytes method). And since qos is not an integer literal that works fine without a space. This is all standard Python behaviour.

Himura2la · 2017-09-26

Thanks for explanation, there are some quite confusing dark corners of Python...

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied