emqx / CocoaMQTT

MQTT 5.0 client library for iOS and macOS written in Swift
https://www.emqx.com/en
Other
1.6k stars 419 forks source link

Decode fails if Variable Byte Integer is greater than 127. #544

Closed dahuayuan closed 1 year ago

dahuayuan commented 1 year ago

According to MQTT 5 protocol https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html, chapter, 1.5.5 Variable Byte Integer

The Variable Byte Integer is encoded using an encoding scheme which uses a single byte for values up to 127. Larger values are handled as follows. The least significant seven bits of each byte encode the data, and the most significant bit is used to indicate whether there are bytes following in the representation. Thus, each byte encodes 128 values and a "continuation bit". The maximum number of bytes in the Variable Byte Integer field is four. The encoded value MUST use the minimum number of bytes necessary to represent the value [MQTT-1.5.5-1]. This is shown in Table 1‑1 Size of Variable Byte Integer.

image

So, if Variable Byte Integer is greater than 127, the length will occupy 2 bytes instead of 1.

Hence , here pos always increase 1 is not correct.

image