Reputation: 849
So I tried running our app with "Address Sanitizer" enabled. And I got this crash:
let sData = "-e5069fba-3612".data(using:String.Encoding.utf8)!
var pointer = sData.withUnsafeBytes {(bytes: UnsafePointer<CChar>) -> UnsafePointer<CChar> in
return bytes
}
pointer = pointer.advanced(by: 1)
let tmpPIN = String(cString: pointer)
print(tmpPIN)
the crash points to let tmpPIN = String(cString: pointer)
. Does anyone know the reason behind this? I can't figure out why this is happening.
Note, the app runs fine when I disabled the "Address Sanitizer". Should I be worry about this or just ignore it?
Upvotes: 0
Views: 586
Reputation: 70976
It seems you found an answer that works, but I'm adding one because I'm still kind of baffled by such complex code for such a simple problem.
Your code:
Data
object,String
. Your fix makes it even more complex by appending an extra byte you don't even want (it works because C strings are expected to have a null character at the end, and your fix adds that).
This could be done far more simply as:
let sData = "-e5069fba-3612"
let tmpPIN = sData2.dropFirst()
The result is exactly the same.
Or you could handle multiple -
characters at the start with something like
let tmpPIN = sData.drop { $0 == "-" }
Which gives the same result for this string.
Upvotes: 2
Reputation: 849
I found this thread... When I add sData.append(0)
after I initialize the sData the Address Sanitizer error is gone.
Upvotes: 0