Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions src/libraries/Common/src/System/IO/Compression/ZLibNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Comment thread
rzikm marked this conversation as resolved.
EnsureState(State.InitializedForDeflate);

fixed (ZStream* stream = &_zStream)
fixed (ZStream* stream = &_zStream)
{
return Interop.ZLib.Deflate(stream, flush);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And here

Expand Down Expand Up @@ -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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does the fix need to be applied here as well?

Expand Down
Loading