Consumer callback is called infinitely while extracting compressed 0byte entry
Summary
Extract a compressed 0byte file, Consumer callback is called infinitely.
I write my code using entry.uncompressedSize for bufferSize to reduce count of consumer closure is called. (like below test)
My app has infinitely loop.
Steps to Reproduce
This test never stop.
Put testExtractCopressedEmptyData.zip to Tests/ZIPFoundationTests/Resources
func testExtractCopressedEmptyData() {
let expectation = XCTestExpectation(description: "Called extract consumer once")
let archive = self.archive(for: #function, mode: .read)
// empty.txt is compressed emtpy file.
guard let entry = archive["empty.txt"] else { XCTFail("Failed to extract entry."); return }
do {
// Use uncompressedSize to reduce count of closure called
let checksum = try archive.extract(entry, bufferSize: UInt32(entry.uncompressedSize)) { (data) in
XCTAssertEqual(data.count, 0)
expectation.fulfill()
}
XCTAssert(entry.checksum == checksum)
} catch {
XCTFail("Failed to unzip data descriptor archive")
}
wait(for: [expectation], timeout: 3.0)
}
To stop infinity loop, I can rewrite the extract code:
let checksum = try archive.extract(entry, bufferSize: max(UInt32(entry.uncompressedSize), 1)) { /* consumer code */ }
However, consumer calls twice. (It should be once)
With Xcode debugger, I notice that the test calls Data.process method with bufferSize = 0.
Data.process creates 0-byte Data inifinity.
NOTE: macOS can creates Zip file have 0byte compressed entry. I create sample zip file and attach in this issue. It contains 0 byte "empty.txt" and 86336byte "faust.txt" (zip command creates stored no-compressed entry for 0 byte file)
$ unzip -v Tests/ZIPFoundationTests/Resources/testExtractCopressedEmptyData.zip
Archive: Tests/ZIPFoundationTests/Resources/testExtractCopressedEmptyData.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
0 Defl:N 2 0% 10-23-2020 16:13 00000000 empty.txt
222214 Defl:N 86336 61% 06-19-2014 17:09 af9c0a0d faust.txt
-------- ------- --- -------
222214 86338 61% 2 files
Method Defl:N means deflated entry.
Expected Results
consumer closure called only once with empty data (Data())
Actual Results
consumer closure called infinitely.
Regression & Version
I am using v0.9.11 Xcode 12.2 & macOS 10.15.7
Related Link
@weichsel I test with current development branch. It causes when entry is file (not directory).
Thanks for updating the issue and for providing a test file.
compressedSize being bigger than uncompressedSize for zero-length entries is an edge case I didn't think about 🙈.
I can't promise when I'll find time to fix this, but if you need this, you can submit a PR and I'll do my best to review it.