emqx / CocoaMQTT

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

Crash in CocoaMQTTStorage due to force unwrapping #484

Closed FadyAckad closed 1 year ago

FadyAckad commented 1 year ago

Platform

OSX

Description

On macOS HighSierra, there's a bug in UserDefaults which causes it to lose its data and stop functioning until the machine is restarted or the process cfprefsd is forcibly killed. This causes a crash in MQTTStorage because of the following force unwrapping:

Screenshot 2022-09-27 at 10 36 56

There is also a way to reproduce it on newer OSX version:

  1. Run the app
  2. Open the terminal and run the command $ defaults delete your.app.bundle.id

This also causes the app to crash immediately

leeway1208 commented 1 year ago

Hi @FadyAckad How about providing a default value just like this?

image

or do u have some good ideas? Thanks.

FadyAckad commented 1 year ago

Hello, Thank you for the quick reply. Sadly, we're still using version 3. So having it fallback to 3.1.1 would be best for us at the moment, but is it possible to avoid using UserDefaults here altogether and would having this flag as a property in the class work? From what I see the version is always set during the initialization of the client. So for example would the following snippet work?

private var mqttVersion: String = ""

func setMQTTVersion(_ version : String) {
    mqttVersion = version
}

func queryMQTTVersion() -> String {
    return mqttVersion
}
leeway1208 commented 1 year ago

Hi. I write the default value for version which is 3.1.1 and also add static function to set version. Please using setMqtt3Version() or setMqtt5Version(). Thanks.

gestas-projects commented 1 year ago

Hi, when this issue will be released?