1024jp / GzipSwift

Swift package that enables gzip/gunzip Data using zlib
MIT License
544 stars 134 forks source link

Runtime crash with Xcode 9.1 #22

Closed banjun closed 7 years ago

banjun commented 7 years ago

I run into random runtime crashes on deflate(&stream, Z_FINISH) and inflate(&stream, Z_SYNC_FLUSH) when I build with Xcode 9.1.0.

Environment:

Xcode: Version 9.1 (9B55) iPhone 8: iOS 11.0.3 (15A432) GzipSwift: 4.0.0 (cocoapods)

minimum example project:

https://github.com/banjun/GzipSwiftXcode910

When Build & Run the project, randomly crash on the deflate or the inflate (or may cause malformed gunzipped data...?).

I have tried so far, Xcode 9.1 Simulator does not crash. also, building with Xcode 9.0.1 cannot cause crashes.

Reason why the example project uses fragmented data

URLSession.dataTask response with data as OS_dispatch_data referencing numbers of 8K block data. I think this is one of the main usecases of gzip.

Analysis

withUnsafeBytes codes are suspicious, especially at https://github.com/1024jp/GzipSwift/blob/72e16b884600b98ac77a871daecaea34aec87f8e/Sources/Data%2BGzip.swift#L279

withUnsafe... methods are not responsible for pointer uses outside the trailing closure. it actually make creating Data -> z_stream difficult. Some memory copies into a new Data with contiguous memory can resolve the crashes.

1024jp commented 7 years ago

Thank you for the report and the pull-request. I've just released GzipSwift 4.0.3 with your fix.