NordicSemiconductor / IOS-nRF-Toolbox

The nRF Toolbox is a container app that stores your Nordic Semiconductor apps for Bluetooth Low Energy in one location.
https://www.nordicsemi.com/Software-and-tools/Development-Tools/nRF-Toolbox
BSD 3-Clause "New" or "Revised" License
590 stars 205 forks source link

App crashing when it encounters badly configured peripheral. #97

Closed AHurnell closed 9 months ago

AHurnell commented 4 years ago

I've noticed the app crashing with the following error message...

"Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)" error in utilities/CharacteristicReader.swift at line 42 "let val = aPointer.pointee".

I think if the peripheral device feeds the app bad data it causes this crash. I think this could be handled more safely by checking that aPointer.pointee is a meaningful value of the correct type.

The badly configured peripheral I'm using is a particle argon using a health thermometer example. The exact sequence of events that causes this crash is that

1) The peripheral is connected. 2) The firmware on the peripheral changes to a different file. 3) The phone app attempts to reconnect, I suspect it attempts to use "apointer" value that it has in a cache. 4) The battery level cannot be checked because "apointer" is not set.

You can overcome it by setting val to 0 and connecting to the device. After that val can be returned to "aPointer.pointee" and the app works fine.

So to fix this issue I think the app should check that "apointer.pointee" is a meaningful value, and if not set val to 0. I'll try to do that.

AHurnell commented 4 years ago

The problem is that if "aPointer.pointee" doesn't have a real value the app crashes. I've added an if function that checks the memory address of aPointer. Perhaps there is a more elegant way of doing this that someone could suggest.

I think that the function "readUInt8Value" in characteristic reader should be changed to this...

static func readUInt8Value(ptr aPointer : inout UnsafeMutablePointer) -> UInt8 { var val:UInt8 = 0 if(String(describing:aPointer)=="0x00000000"){ print("not running val = aPointer.pointee here prevents an error.") }else{ val = aPointer.pointee } aPointer = aPointer.successor() return val }

AHurnell commented 4 years ago

I found another example of a similar issue. In the CSCViewController.swift file around line 346.

So there's a general problem with unsafemutable pointers, if they are undefined a device can crash the app.

NickKibish commented 9 months ago

Thank you for the report. v6 of nRF-Toolbox is under development. It will be fixed there.