fix: free native context if a stream constructor fails after creating it#13
Merged
Conversation
The four streaming wrappers create a native ZSTD_C/DCtx and then make a further native call (set parameter / load dictionary). If that second call threw, the freshly created context was leaked: the java.io streams closed only the arena (not the cctx/dctx), and the segment drivers rethrew from create() before super() could take ownership. Free the context on every constructor error path, and move the java.io streams' post-setup buffer allocations inside the guarded block so a failure there frees the context and the arena too. Found during a memory-leak / security review of the zstd module. The rest of the module checked out: NativeObject's atomic-swap close is exactly-once and idempotent; the ctx/dict creators allocate the native handle as their last step; copy helpers guard with toIntExact and requireNative; decode entry points validate bounds and native-ness. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Segment drivers now take ownership first (super(createCtx())) and run setup in the constructor body, so a setup failure is released by the existing close() — drops the duplicated free helper. The java.io streams share one freeCctx/freeDctx best-effort helper between the constructor error path and close(). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In-depth memory-leak / security review of the
zstdmoduleBug found & fixed — native context leak on constructor failure
All four streaming wrappers —
ZstdOutputStream,ZstdInputStream,ZstdCompressStream,ZstdDecompressStream— create a nativeZSTD_CCtx/ZSTD_DCtxand then make a second native call (set compression level / load dictionary). If that second call threw (e.g. an out-of-range parameter, a malformed dictionary, OOM), the just-created context leaked:java.iostreams closed only theArenain theircatch, never thecctx/dctx;create()helper beforesuper(...)could hand the pointer toNativeObjectfor tracked cleanup.Fix: free the context on every constructor error path. For the
java.iostreams the post-setup buffer allocations also moved inside the guarded block, so a failure there releases both the context and the arena. Low-probability paths, but a real native leak each time.Reviewed and found clean
NativeObject—AtomicReferenceswap toNULLmakestryCloseexactly-once and idempotent under repeated/concurrent close.ZstdCompressCtx/ZstdDecompressCtx/ZstdCompressDict/ZstdDecompressDict) — allocate the native handle as the last step, so an earlier failure leaks nothing.Zstd.copyIn/copyOut—Math.toIntExactguards the >2 GiB overflow,max(len,1)avoids zero-length allocation.requireNativeon every segment,Objects.checkFromIndexSizeon array ranges, NULL-pointer checks on create, negative-sentinel handling viaZstd.call.No integer-overflow, TOCTOU (beyond the already-hardened loader), or unbounded-read issues found (
errorNamereads a static zstd string).Test
./mvnw -pl zstd,integration-tests -am verify— 138 unit + 79 integration green, checkstyle + javadoc clean.🤖 Generated with Claude Code