emqx / CocoaMQTT

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

"Index out of range" - error occurs when passing user properties to message #576

Open ltAldoRaine opened 4 months ago

ltAldoRaine commented 4 months ago

Hello, I'm trying to pass user properties to my publish message, but my application crashes with the error - Index Out of Range.

The error occurs in this method.

func unsignedByteToString(data:[UInt8], offset:Int) -> (resStr: String, newOffset: Int)?{
    var newOffset = offset

    if offset + 1 > data.count {
        return nil
    }

    var length = 0
    let comRes = integerCompute(data: data, formatType: formatInt.formatUint16.rawValue, offset: newOffset)
    length = comRes!.res
    newOffset = comRes!.newOffset

    var stringData = Data()
    for _ in 0 ..< length {
        stringData.append(**data[newOffset]**) // here
        newOffset += 1
    }
    guard let res = String(data: stringData, encoding: .utf8) else {
        return nil
    }

    return (res, newOffset)
}