Repository navigation
Surface real callback exceptions in ZipArchiveReader instead of "Code = -100" - #105103
Conversation
|
Pre-PR validation gate (session a) Deterministic repro? ✅ The new gtest b) Root cause explained? ✅ The minizip stream callbacks ( c) Fix matches root cause? ✅ The captured exception is rethrown at exactly the two sites where it was being dropped: (1) d) Test intent preserved / new tests added? ✅ All 79 existing archive gtests continue to pass. A new gtest specifically covers the laundering path the issue describes. e) Both directions demonstrated? ✅ Without the fix the new gtest fails (got code 643 f) Fix is general? ✅ The exception capture now happens in every callback that catches an exception ( This is a defensive fix that improves the user-facing error message. It does not by itself change the underlying behavior of |
|
cc @PedroTadim @rschu1ze @alexey-milovidov — could you review this? It surfaces the real underlying exception (e.g. |
|
Workflow [PR], commit [19c58ef] Summary: ✅
AI ReviewSummaryThis PR preserves Final VerdictStatus: ✅ Approve |
…-scope clickhouse-gh[bot] flagged on PR ClickHouse#105103 that the `Opaque` struct used by the minizip stream callbacks was a stack-local in `StreamFromReadBuffer::open`. Minizip stores the `opaque` pointer internally during `unzOpen2_64` and dereferences it from every later callback (`readFileFunc`, `seekFunc`, `tellFunc`); after `open` returned successfully, those callbacks dereferenced freed stack memory whenever they took the exception-capture path through `storeException(strm, opaque)`. ASan confirms the bug: ERROR: AddressSanitizer: stack-use-after-return READ of size 8 at 0x... thread T0 Address ... is located in stack of thread T0 at offset 88 in frame ... [64, 96) 'opaque' (line 74) <== Memory access at offset 88 is inside this variable The fix heap-allocates `Opaque` via `std::shared_ptr` and pins it for the lifetime of the `unzFile` handle: * `OpenResult` carries a `std::shared_ptr<Opaque>` owning the storage. * `RawHandleWithStream` (in the header) gains a `std::shared_ptr<void> opaque` field so the handle cache keeps the storage alive across acquire/release. * `HandleHolder` moves the opaque shared pointer with the handle and passes it back to the cache in `releaseRawHandle()`. `closeFileFunc` already ignores the `opaque` parameter, so destruction order during `unzClose()` in `~ZipArchiveReader` is safe: minizip releases the handle first, then the shared pointers in `free_handles` drop the `Opaque`. A new gtest, `ZipArchiveReaderTest.RethrowsCallbackExceptionAfterOpenSucceeded`, opens the archive normally, then flips an externally-owned flag so the next post-open callback throws. Under ASan, this test deterministically fails on the pre-fix code (stack-use-after-return at line 74) and passes cleanly with the heap-allocated `Opaque`.
|
Addressed @clickhouse-gh[bot]'s memory-safety review in commit 49c0890. Bot's concern (correct): Confirmed with ASan. Wrote a regression test ( Fix. Moved
Destruction order is safe because Verification (both directions):
Session: cron:clickhouse-ci-task-worker:20260516-060000 |
|
Pre-PR Validation Gate (per TASK.md Phase 4 Step 9)
Local verification:
Session: cron:clickhouse-ci-task-worker:20260516-060000 |
|
This was fixed by #105146. Let's update the branch. |
… = -100" When reading a zip archive whose underlying `SeekableReadBuffer` throws (for example `ReadBufferFromS3` with `restricted_seek = true` rejecting a backward seek during minizip's EOCD search), the exception was caught inside the `StreamFromReadBuffer` C callbacks, the callback returned an error code, and minizip's state machine then surfaced a generic `MZ_END_OF_LIST` (-100). The result was the unhelpful `Couldn't unpack zip archive 'X': Code = -100` message reported in issue ClickHouse#104681, with the actual cause (e.g. `CANNOT_SEEK_THROUGH_FILE`) silently dropped. The fix: * `HandleHolder::checkResult` and `showError` now rethrow any stored stream exception before falling through to the cryptic minizip error formatter, so every post-open failure surfaces the underlying error instead of `Code = -100`. * `StreamFromReadBuffer::open()` returns the captured exception when `unzOpen2_64` fails. Minizip cleans up the stream during its own failure path, so the in-stream `stored_exception` is unreachable afterwards. The read/seek/tell callbacks therefore also write a copy of the exception into the shared `Opaque` struct (lives on the caller's stack), which `acquireRawHandle` rethrows instead of the generic `Couldn't open zip archive` message. A new gtest reproduces the issue with a `SeekFailingReadBuffer` that fails on the second seek after a read; with the fix the underlying `CANNOT_SEEK_THROUGH_FILE` is surfaced unchanged. Closes ClickHouse#104681
…-scope clickhouse-gh[bot] flagged on PR ClickHouse#105103 that the `Opaque` struct used by the minizip stream callbacks was a stack-local in `StreamFromReadBuffer::open`. Minizip stores the `opaque` pointer internally during `unzOpen2_64` and dereferences it from every later callback (`readFileFunc`, `seekFunc`, `tellFunc`); after `open` returned successfully, those callbacks dereferenced freed stack memory whenever they took the exception-capture path through `storeException(strm, opaque)`. ASan confirms the bug: ERROR: AddressSanitizer: stack-use-after-return READ of size 8 at 0x... thread T0 Address ... is located in stack of thread T0 at offset 88 in frame ... [64, 96) 'opaque' (line 74) <== Memory access at offset 88 is inside this variable The fix heap-allocates `Opaque` via `std::shared_ptr` and pins it for the lifetime of the `unzFile` handle: * `OpenResult` carries a `std::shared_ptr<Opaque>` owning the storage. * `RawHandleWithStream` (in the header) gains a `std::shared_ptr<void> opaque` field so the handle cache keeps the storage alive across acquire/release. * `HandleHolder` moves the opaque shared pointer with the handle and passes it back to the cache in `releaseRawHandle()`. `closeFileFunc` already ignores the `opaque` parameter, so destruction order during `unzClose()` in `~ZipArchiveReader` is safe: minizip releases the handle first, then the shared pointers in `free_handles` drop the `Opaque`. A new gtest, `ZipArchiveReaderTest.RethrowsCallbackExceptionAfterOpenSucceeded`, opens the archive normally, then flips an externally-owned flag so the next post-open callback throws. Under ASan, this test deterministically fails on the pre-fix code (stack-use-after-return at line 74) and passes cleanly with the heap-allocated `Opaque`.
8aaf7e2 to
19c58ef
Compare
|
Rebased onto current master to resolve a merge conflict in The conflict was trivial: master (#106849) switched the archive reader to The history is now clean (2 commits, the stale merge commit was dropped). Verified locally with clang-21 debug:
Session: cron:clickhouse-worker-slot-7:20260610-212500 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 152/165 (92.12%) | Lost baseline coverage: none · Uncovered code |
|
CI finished on `19c58efab5c0` with no test failures. Ready for review. |
…-scope clickhouse-gh[bot] flagged on PR ClickHouse#105103 that the `Opaque` struct used by the minizip stream callbacks was a stack-local in `StreamFromReadBuffer::open`. Minizip stores the `opaque` pointer internally during `unzOpen2_64` and dereferences it from every later callback (`readFileFunc`, `seekFunc`, `tellFunc`); after `open` returned successfully, those callbacks dereferenced freed stack memory whenever they took the exception-capture path through `storeException(strm, opaque)`. ASan confirms the bug: ERROR: AddressSanitizer: stack-use-after-return READ of size 8 at 0x... thread T0 Address ... is located in stack of thread T0 at offset 88 in frame ... [64, 96) 'opaque' (line 74) <== Memory access at offset 88 is inside this variable The fix heap-allocates `Opaque` via `std::shared_ptr` and pins it for the lifetime of the `unzFile` handle: * `OpenResult` carries a `std::shared_ptr<Opaque>` owning the storage. * `RawHandleWithStream` (in the header) gains a `std::shared_ptr<void> opaque` field so the handle cache keeps the storage alive across acquire/release. * `HandleHolder` moves the opaque shared pointer with the handle and passes it back to the cache in `releaseRawHandle()`. `closeFileFunc` already ignores the `opaque` parameter, so destruction order during `unzClose()` in `~ZipArchiveReader` is safe: minizip releases the handle first, then the shared pointers in `free_handles` drop the `Opaque`. A new gtest, `ZipArchiveReaderTest.RethrowsCallbackExceptionAfterOpenSucceeded`, opens the archive normally, then flips an externally-owned flag so the next post-open callback throws. Under ASan, this test deterministically fails on the pre-fix code (stack-use-after-return at line 74) and passes cleanly with the heap-allocated `Opaque`.
…hive-exception-laundering Surface real callback exceptions in ZipArchiveReader instead of "Code = -100"
When reading a zip archive whose underlying
SeekableReadBufferthrows (for exampleReadBufferFromS3withrestricted_seek = truerejecting a backward seek during minizip's EOCD search), the exception was caught inside theStreamFromReadBufferC callbacks, the callback returned an error code, and minizip's state machine then surfaced a genericMZ_END_OF_LIST(-100). The result was the unhelpfulreported in #104681, with the actual cause (e.g.
CANNOT_SEEK_THROUGH_FILE) silently dropped.What changed
HandleHolder::checkResultandshowErrornow rethrow any stored stream exception before falling through to the cryptic minizip error formatter, so every post-open failure surfaces the underlying error instead ofCode = -100.StreamFromReadBuffer::openreturns the captured exception whenunzOpen2_64fails. Minizip cleans up the stream during its own failure path, so the in-streamstored_exceptionis unreachable afterwards. The read/seek/tell callbacks therefore also write a copy of the exception into the sharedOpaquestruct (lives on the caller's stack), whichacquireRawHandlerethrows instead of the genericCouldn't open zip archivemessage.Testing
A new gtest
ZipArchiveReaderTest.RethrowsCallbackExceptionOnOpenreproduces the issue with aSeekFailingReadBufferthat fails on the second seek after a read. With the fix the underlyingCANNOT_SEEK_THROUGH_FILEis surfaced unchanged; without the fix the test fails (gets code 643 / "Couldn't open zip archive" instead of expected 87 /CANNOT_SEEK_THROUGH_FILE).All 79 existing archive gtests continue to pass.
Closes #104681
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Surface the real underlying exception when a zip archive cannot be unpacked because the input buffer (e.g. an S3 read buffer with
restricted_seek = true) refuses a seek or read requested by minizip. Previously the actual error was hidden behind the genericCouldn't unpack zip archive: Code = -100/Couldn't open zip archivemessage.Version info
26.7.1.126