mw99 / DataCompression

Swift libcompression wrapper as an extension for the Data type (GZIP, ZLIB, LZFSE, LZMA, LZ4, deflate, RFC-1950, RFC-1951, RFC-1952)
Apache License 2.0
286 stars 57 forks source link

fatalErrors on XCode14/iOS16 #32

Closed travisgriggs closed 1 year ago

travisgriggs commented 2 years ago

Been using this happily for years. Updated phone to iOS 16 and XCode to 14 today and now I'm getting fatalErrors in

func unzip(skipCheckSumValidation: Bool = true) -> Data?

Somewhere inside the

return last.withMemoryRebound(to: UInt32.self, capacity: 1)

statement. I don't get a lot of useful info from it, just

Swift/arm64-apple-ios.swiftinterface:23320: Fatal error
2022-09-14 16:03:58.690178-0700 TWiG MC[668:25066] Swift/arm64-apple-ios.swiftinterface:23320: Fatal error

in my console. My Data argument headed in was:

78.DA.0D.CD.3B.4E.43.31.10.40.51.B3.81.B4.94.D0.23.9E.C6.E3.4F.6C.29.EB.A0.45.E3.F9.C0.53.88.8D.EC.14.B0.FB.BC.F6.16.E7.3E.7D.5E.DC.29.55.2D.6C.31.A8.17.0B.A1.24.3A.37.23.00.89.39.86.AC.18.6B.33.03.C5.1C.19.53.40.CB.D2.72.80.7A.14.9F.C8.17.41.36.16.43.4E.44.29.85.5C.9B.A8.86.B3.C5.C4.B1.98.04.40.AA.15.98.35.A2.57.F6.26.02.10.92.02.D6.82.8D.51.01.DC.2F.AD.75.D5.7F.EF.5E.9A.B7.63.17.A9.94.06.87.AB.DF.63.DD.77.71.CF.DB.EB.C7.BB.07.78.EB.74.53.77.9A.8B.3A.FD.E9.DA.BA.FE.AC.D1.F7.39.F7.2F.BA.EF.A3.6F.3C.6E.AE.CD.71.D5.E9.DC.03.5C.71.40.90

I'll do some digging/experimenting. This has been rocksolid for so long, surprised to see it start misbehaving all of a sudden

travisgriggs commented 2 years ago

Based on people referencing a note in the doc that says you're not supposed to rebind pointers to different size pointers using withMemoryRebound, I rewrote the cksum computation as follows:

let cksum: UInt32 = withUnsafeBytes { (bytePtr: UnsafePointer<UInt8>) -> UInt32 in
    let uint32Pointer = UnsafeRawPointer(bytePtr.advanced(by: count - 4)).bindMemory(to: UInt32.self, capacity: 1)
    return uint32Pointer.pointee.bigEndian
}

I'm not a super low-level Swift guy, so I don't know if this is a "good" solution. I can do a pull-request if that is an approved approach. I just modified the unzip() function. I notice that there is another call of withMemoryRebound in gunzip() that rebinds bytes as shorts which presumably will break now too.

mw99 commented 2 years ago

Hi, big thanks for the feedback. I thought this problem was resolved with the 3.7.0 release, seems not to be the case. I only tried with the Xcode beta until now and with that I had no issues. I assume you are using the latest version 3.7.0 right?

I will take a closer look at this problem in the coming days. A pull request is not needed, you already provided more than I could ask for. Thanks.

mw99 commented 2 years ago

Hi again, can you test for me if the unit test pass on your machine? Just a git clone + swift test on the command line. Because I get no error with Xcode 14.

mw99 commented 1 year ago

With some deeper digging I found a couple of unaligned pointer problems causes by internal changes of Swift 5.7 They should be resolved with the latest release of version 3.8.0 Please check: https://github.com/mw99/DataCompression/releases/tag/3.8.0

If the problems come up again, please open a new issue! Big thanks again for the error reporting.