Skip to content

suback parse wrong if resonCode bigger than 128 #609

@lijiahao007

Description

@lijiahao007
extension FrameSubAck: InitialWithBytes {
    
    init?(packetFixedHeaderType: UInt8, bytes: [UInt8]) {
        
        self.packetFixedHeaderType = packetFixedHeaderType

        var protocolVersion = "";
        if let storage = CocoaMQTTStorage() {
            protocolVersion = storage.queryMQTTVersion()
        }

        if (protocolVersion == "5.0"){
            // the bytes length must bigger than 3
            guard bytes.count >= 4 else {
                return nil
            }

            self.msgid = UInt16(bytes[0]) << 8 + UInt16(bytes[1])
            self.grantedQos = []
            for i in 3 ..< bytes.count {
                guard let qos = CocoaMQTTQoS(rawValue: bytes[i]) else {
                    return nil
                }
                self.grantedQos.append(qos)
            }

            self.reasonCodes = [CocoaMQTTSUBACKReasonCode]()
            for i in 3 ..< bytes.count {
                guard let qos = CocoaMQTTSUBACKReasonCode(rawValue: bytes[i]) else {
                    return nil
                }
                self.reasonCodes! += [qos]
            }

            self.subAckProperties = MqttDecodeSubAck()
            self.subAckProperties!.decodeSubAck(fixedHeader: packetFixedHeaderType, pubAckData: bytes)

        }else{
            // the bytes length must bigger than 3
            guard bytes.count >= 3 else {
                return nil
            }

            self.msgid = UInt16(bytes[0]) << 8 + UInt16(bytes[1])
            self.grantedQos = []
            for i in 2 ..< bytes.count {
                guard let qos = CocoaMQTTQoS(rawValue: bytes[i]) else {
                    return nil
                }
                self.grantedQos.append(qos)
            }

        }
        
    }
}

if bytes is [0, 1, 0, 135], this function return nil。Because CocoaMQTTQoS(rawValue: bytes[i]) return nil

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions