Skip to content

Incorrect remaining length is calculated when topic contains utf-8-3 or utf-8-4 characters in SUBSCRIBE/PUBLISH packets #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Eason010212 opened this issue Aug 27, 2021 · 3 comments

Comments

@Eason010212
Copy link
Contributor

Eason010212 commented Aug 27, 2021

Current version of adafruit_minimqtt.py uses len() method to calculate a packet's remaining length, for example:

packet_length += sum(len(topic) for topic, qos in topics)
remaining_length = 2 + len(msg) + len(topic)

As shown above, strings are measured instead of UTF-8 bytes. When a topic only contains English characters, numbers and punctuation marks, the calculation result is correct; However, when it contains some utf-8-3 or utf-8-4 characters (such as Chinese characters) , an incorrect calculation result of remaining length will be obtained. For example, the actual encoding length of "中文" should be 6, while the length calculated by len() is 2. As a result, such packets will be rejected by any MQTT server.

In MQTT protocol, there is no restriction on utf-8-3 or utf-8-4 characters, so I think this issue is probably a bug that needs to be fixed.

At present, in the application I developed, I rewrite the len() method to temporarily solve this problem, but I still hope that the developers of this library can make official bug fixes.

Thank you very much.

@Eason010212 Eason010212 changed the title Incorrect variable length field in SUBSCRIBE/PUBLISH packets are generated when topic contains utf-8-3 or utf-8-4 characters Incorrect variable length in SUBSCRIBE/PUBLISH packet is calculated when topic contains utf-8-3 or utf-8-4 characters Aug 27, 2021
@Eason010212 Eason010212 changed the title Incorrect variable length in SUBSCRIBE/PUBLISH packet is calculated when topic contains utf-8-3 or utf-8-4 characters Incorrect variable length is calculated when topic contains utf-8-3 or utf-8-4 characters in SUBSCRIBE/PUBLISH packets Aug 27, 2021
@Eason010212 Eason010212 changed the title Incorrect variable length is calculated when topic contains utf-8-3 or utf-8-4 characters in SUBSCRIBE/PUBLISH packets Incorrect remaining length is calculated when topic contains utf-8-3 or utf-8-4 characters in SUBSCRIBE/PUBLISH packets Aug 27, 2021
@brentru
Copy link
Member

brentru commented Aug 27, 2021

@Eason010212 Hi - are you interested in submitting this as a pull request? I'll review it.

@Eason010212
Copy link
Contributor Author

@Eason010212 Hi - are you interested in submitting this as a pull request? I'll review it.
OK

FoamyGuy added a commit that referenced this issue Dec 22, 2021
@FoamyGuy
Copy link
Contributor

closed by #91

rtwfroody pushed a commit to rtwfroody/Adafruit_CircuitPython_MiniMQTT that referenced this issue Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants