-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Fix race between zlib Inflate/Deflate and Dispose #128752
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -288,11 +288,6 @@ public uint AvailOut | |
| set { _zStream.availOut = value; } | ||
| } | ||
|
|
||
| private void EnsureNotDisposed() | ||
| { | ||
| ObjectDisposedException.ThrowIf(InitializationState == State.Disposed, this); | ||
| } | ||
|
|
||
| private void EnsureState(State requiredState) | ||
| { | ||
| if (InitializationState != requiredState) | ||
|
|
@@ -348,18 +343,28 @@ private unsafe void DeflateInit2_(CompressionLevel level, int windowBits, int me | |
|
|
||
| public unsafe ErrorCode Deflate(FlushCode flush) | ||
| { | ||
| EnsureNotDisposed(); | ||
| EnsureState(State.InitializedForDeflate); | ||
| bool refAdded = false; | ||
| try | ||
| { | ||
| DangerousAddRef(ref refAdded); | ||
| EnsureState(State.InitializedForDeflate); | ||
|
|
||
| fixed (ZStream* stream = &_zStream) | ||
| fixed (ZStream* stream = &_zStream) | ||
| { | ||
| return Interop.ZLib.Deflate(stream, flush); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the Zlib implementation robust against multiple threads calling Deflate at the same time on the same state? It is fine for it to produce corrupted results, but it is not fine for it to crash or corrupt memory that it does not own. |
||
| } | ||
| } | ||
| finally | ||
| { | ||
| return Interop.ZLib.Deflate(stream, flush); | ||
| if (refAdded) | ||
| { | ||
| DangerousRelease(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public unsafe ErrorCode DeflateEnd() | ||
| { | ||
| EnsureNotDisposed(); | ||
| EnsureState(State.InitializedForDeflate); | ||
|
|
||
| fixed (ZStream* stream = &_zStream) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here |
||
|
|
@@ -395,29 +400,50 @@ private unsafe void InflateInit2_(int windowBits) | |
|
|
||
| public unsafe ErrorCode InflateReset2_(int windowBits) | ||
| { | ||
| EnsureNotDisposed(); | ||
| EnsureState(State.InitializedForInflate); | ||
| bool refAdded = false; | ||
| try | ||
| { | ||
| DangerousAddRef(ref refAdded); | ||
| EnsureState(State.InitializedForInflate); | ||
|
|
||
| fixed (ZStream* stream = &_zStream) | ||
| fixed (ZStream* stream = &_zStream) | ||
| { | ||
| return Interop.ZLib.InflateReset2_(stream, windowBits); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| return Interop.ZLib.InflateReset2_(stream, windowBits); | ||
| if (refAdded) | ||
| { | ||
| DangerousRelease(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public unsafe ErrorCode Inflate(FlushCode flush) | ||
| { | ||
| EnsureNotDisposed(); | ||
| EnsureState(State.InitializedForInflate); | ||
| bool refAdded = false; | ||
| try | ||
| { | ||
| DangerousAddRef(ref refAdded); | ||
| EnsureState(State.InitializedForInflate); | ||
|
|
||
| fixed (ZStream* stream = &_zStream) | ||
| fixed (ZStream* stream = &_zStream) | ||
| { | ||
| return Interop.ZLib.Inflate(stream, flush); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| return Interop.ZLib.Inflate(stream, flush); | ||
| if (refAdded) | ||
| { | ||
| DangerousRelease(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public unsafe ErrorCode InflateEnd() | ||
| { | ||
| EnsureNotDisposed(); | ||
| EnsureState(State.InitializedForInflate); | ||
|
|
||
| fixed (ZStream* stream = &_zStream) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the fix need to be applied here as well? |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.