Skip to content

Conversation

@edwardneal
Copy link
Contributor

@edwardneal edwardneal commented Apr 18, 2025

Fixes #89445

This reimplements #71991 in Deflater and Inflater. The PR was previously reverted in #85001 as a result of issue #84994.

As noted in the reverting PR, Deflater simply needed to dispose of the returned SafeHandle and supress its own finalizer if CreateZLibStreamForDeflate threw an exception. We now do this.

We couldn't do this for Inflater at the time - it had to support concatenated GZip data, and as part of that support it would recreate its ZLibStreamHandle. As a result of #113587, this recreation no longer happens and we can treat Inflater as we would treat Deflater. I've refactored Inflater.InflateInit out of existence and made _zlibStream readonly to reflect this.

This also incorporates a change to both class' DeallocateBufferHandle methods (when called by Dispose) to prevent them dereferencing a ZLibStreamHandle after it's been disposed of. This is roughly how #84994 was originally discovered.

If the call to CreateZLibStreamForXXflate throws an exception from the constructor, explicitly dispose of the ZLibStreamHandle (rather than relying on the finalizer to do it.)

Also incorporates a change to DeallocateBufferHandle (called from the Dispose path) to prevent it using a ZlibStreamHandle after it's been Disposed.
This aligns Inflater with Deflater.
Removed now-unnecessary usings.
Copy link
Member

@rzikm rzikm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable so far, @edwardneal, are you still looking to contribute this change? Have you had time to look into addressing #89445 as well?

Separates the initialization of a ZLibStreamHandle and only instantiates a Deflater or Inflater instance if this initialization succeeds
Copy link
Member

@rzikm rzikm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good now, besides the few nits from @stephentoub .

Stephen, do you want to take another look?

@rzikm rzikm requested a review from stephentoub August 14, 2025 08:11
@rzikm
Copy link
Member

rzikm commented Sep 8, 2025

@stephentoub I think your comments are addressed now, do you want to give this PR another review?

@rzikm rzikm self-assigned this Sep 11, 2025
@rzikm
Copy link
Member

rzikm commented Sep 29, 2025

@stephentoub Gentle ping in case this slipped your inbox

Moved error handling to XxflateInit2_. These methods will either return successfully or throw an exception.

Resource string and code style changes.
@rzikm
Copy link
Member

rzikm commented Nov 10, 2025

Hey @edwardneal, there still seem to be some unaddressed code review feedback, can you look into it?

@edwardneal
Copy link
Contributor Author

There was one review comment on the name for the exception in CreateForDeflate (and a few others) which I'd corrected, but not resolved the comment, apologies.

Another two comments (the string resource and the reference) are covered in this response. I think I've answered the question, but I've left it open in case there's anything to follow up.

This leaves some feedback on code duplication between CreateForInflate and CreateForDeflate, which is just waiting for a response here.

@rzikm
Copy link
Member

rzikm commented Nov 28, 2025

Thanks for being patient with us, I think the PR is in good shape to go, I am happy to leave further improvements on #114826 (comment) to a follow up PRs

@rzikm rzikm merged commit c3d9ccd into dotnet:main Nov 28, 2025
86 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.IO.Compression community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix SafeHandle usage in System.IO.Compression

5 participants