aciidgh / SwiftMQTT

MQTT Client in pure swift ❤️
MIT License
270 stars 71 forks source link

Crash when QosValue == 2 #54

Open kenji21 opened 4 years ago

kenji21 commented 4 years ago

When creating:

        let qos = MQTTQoS(rawValue: header.flags & 0x06)!

this force unwrap crashes if QoS on a publish message is 2, to fix this crash:

public enum MQTTQoS: UInt8 {
    case atMostOnce     = 0x0
-    case atLeastOnce    = 0x01
+    case atLeastOnce    = 0x02
-    case exactlyOnce    = 0x02
+    case exactlyOnce    = 0x04
}

Why this crash ? as MQTTQoS is declared as a UInt8, and http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718038 state that QoS bits are:

 3.3.1.2 QoS

Position: byte 1, bits 2-1.

so having 0b0101 00100 on the header flags byte, then ANDing with 0x06 gives 0b0000 0100 (=> 4 decimal)

QoS value 1 (0b0101 00010 & 0x06 => 2 decimal) was working, but using exactlyOnce value.

Should I send a PR for this ?