Skip to content
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

suback parse wrong if resonCode bigger than 128 #609

Open
lijiahao007 opened this issue Sep 11, 2024 · 1 comment
Open

suback parse wrong if resonCode bigger than 128 #609

lijiahao007 opened this issue Sep 11, 2024 · 1 comment

Comments

@lijiahao007
Copy link

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

@lijiahao007
Copy link
Author

Does not seem to respond correctly to the following situation

screenshot

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

1 participant