UnsafePointer Problem on iOS 16.1

Follow code have problem on iOS 16.1. Look like FOOTER_Data got GC immediate.

let FOOTER_Data:Data = Data(bytes: UnsafePointer([0xFF, 0xFF, 0xFF, 0xFF] as [UInt8]), count: 4)

var FOOTER2:[UInt8] = [0xFF, 0xFF, 0xFF, 0xFF] let FOOTER2_Data:Data = Data(bytes: UnsafePointer(FOOTER2 as [UInt8]), count: 4)

let sendData:NSMutableData = NSMutableData(length: 0)!

sendData.append(FOOTER_Data)

sendData.append(FOOTER2_Data)

sendData.append(Data(bytes: &FOOTER2, count: 4))

print("sendData : (sendData)")

// On iOS 16.1 : sendData : {length = 12, bytes = 0x00000000ffffffffffffffff} // Before iOS 16.1 : sendData : {length = 12, bytes = 0xffffffffffffffffffffffff}

When I plug your code into Xcode 14.1 it flags multiple Initialization of 'UnsafePointer<UInt8>' results in a dangling pointer warnings. For more background on that problem, see The Peril of the Ampersand.

Looking through your code I’m at a loss as to what it’s trying to do. However, I’m quite sure that you can do it without any unsafe constructs. Consider this:

let FOOTER_Data = Data([0xFF, 0xFF, 0xFF, 0xFF])
let FOOTER2: [UInt8] = [0xFF, 0xFF, 0xFF, 0xFF]
let FOOTER2_Data = Data(FOOTER2)
var sendData = Data()
sendData.append(contentsOf: FOOTER_Data)
sendData.append(contentsOf: FOOTER2_Data)
sendData.append(contentsOf: FOOTER2)
let sendDataNS = NSMutableData(data: sendData)
print("sendDataNS : \(sendDataNS)")

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The point I want to say is, the follow code I used from 2017 (iOS7) to now until (iOS 16.1).

They changed the behaviour and I used 2 days to find out.

let FOOTER_Data:Data = Data(bytes: UnsafePointer([0xFF, 0xFF, 0xFF, 0xFF] as [UInt8]), count: 4)

The point I want to say is, the follow code I used from 2017 (iOS7) to now until (iOS 16.1).

I’m not sure I understand your point here. I suspect that you’re saying that code that worked on previous systems should continue to work on new systems. That’s true in general, but only if the code is well formed. The code you posted is not well formed. It relies on undefined behaviour. Code that relies on undefined behaviour can change behaviour at any time.

Note that I’m using the term undefined behaviour in a specific technical sense here, as described here.

The best way to avoid undefined behaviour in Swift is:

  • Don’t use constructs tagged as unsafe. That more-or-less eliminates memory safety problems.

  • Adopt Swift concurrency. That more-or-less eliminates threading problems.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

print("sendData : ((sendData)")

print("sendData : (\(sendData)")

Yes, I know that is wrong as now day, as it was within the function some like Math.max that I think never will go wrong. That make me used more time to debug it.

And I send it here is hope someone like my situation will know that early.

I already changed all code using unsafe pointer and add new test case for those function.

Thanks for all of your reply.

Tony.

And I send it here is hope someone like my situation will know that early.

The code you posted generated a compiler warning when I compiled it. Was it generating that warning in your environment?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

UnsafePointer Problem on iOS 16.1
 
 
Q