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

Comment on lines 220 to 227
if (errC is not ErrorCode.Ok)
{
string zlibErrorMessage = zLibStreamHandle.GetErrorMessage();
string exceptionMessage = GenerateExceptionMessage(errC);

zLibStreamHandle.Dispose();
throw new ZLibException(exceptionMessage, "inflateInit2_", (int)errC, zlibErrorMessage);
}
Copy link
Member

Choose a reason for hiding this comment

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

Is this identical to the same block in the deflate method, other than the const function name passed to the throw? Can you lift this into a shared helper? Could you dedup both methods almost entirely with a delegate that's just used to parameterize the Deflate/InflateInit2 call? You could have that delegate accept a tuple to avoid a closure allocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've created the shared helper and moved the logic down into DeflateInit2_ and InflateInit2_, and this renders CreateForDeflate and CreateForInflate nearly identical.

Do you mind if I deal with the duplication in a future PR? I'm planning to adjust ZLibStreamHandle and the PAL so that it uses its handle field and we can marshal usable handles directly, and this'll change these methods anyway.

</data>
<data name="ZLibErrorIncorrectInitParameters" xml:space="preserve">
<value>The underlying compression routine received incorrect initialization parameters.</value>
</data>
Copy link
Member

Choose a reason for hiding this comment

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

Where are these used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We moved the logic in Deflater & Inflaters' constructors which created a ZLibStreamHandle into factory methods on the type. That pushed references to string resources (and to ZLibException) into ZLibNative.cs, and that file is source-included in System.Net.WebSockets.

In theory, this would seem to bring a behaviour change: if a web socket fails to initialize ZLib, the inner exception's message could change from The underlying compression routine returned an unexpected error code: '{0}' to The underlying compression routine received incorrect initialization parameters. or The version of the underlying compression routine does not match expected version. if the error code returned is -2 or -6 respectively. In practice though, this can't happen: the compression routine versions are locked by the version of ZLib we package, and the only initialization parameters are pre-validated in WebSocketDeflateOptions. The resources are just there to overcome a compilation error.

This is also why we have the new reference in System.Net.WebSockets: ZLibException is a public type which doesn't appear in the S.IO.C reference assembly.

<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\src\System.Collections.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.Tracing\src\System.Diagnostics.Tracing.csproj" />
<!-- Bypass System.IO.Compression's reference assembly in order to allow ZLibNative to access the ZLibException type. -->
<ProjectReference Include="$(LibrariesProjectRoot)System.IO.Compression\src\System.IO.Compression.csproj" SkipUseReferenceAssembly="true" />
Copy link
Member

Choose a reason for hiding this comment

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

Is this still needed?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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