Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consumer callback is called infinitely while extracting compressed 0byte entry #191

Open
mtgto opened this issue Oct 23, 2020 · 2 comments

Comments

@mtgto
Copy link
Contributor

mtgto commented Oct 23, 2020

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

@mtgto
Copy link
Contributor Author

mtgto commented Dec 2, 2020

@weichsel I test with current development branch. It causes when entry is file (not directory).

@weichsel
Copy link
Owner

weichsel commented Dec 2, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants