Skip to content

Commit 1c82b73

Browse files
authored
Add stream checks before returning it (#119243)
* Add checks to see if the stream is disposed before returning them * Fix the test since the functions throw ObjectDisposedException now
1 parent 1d56b2a commit 1c82b73

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/libraries/Common/tests/System/IO/Compression/CompressionStreamUnitTestBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,13 @@ public async Task BaseStream_Modify(CompressionMode mode)
441441
[Theory]
442442
[InlineData(CompressionMode.Compress)]
443443
[InlineData(CompressionMode.Decompress)]
444-
public void BaseStream_NullAfterDisposeWithFalseLeaveOpen(CompressionMode mode)
444+
public void BaseStream_ThrowsAfterDisposeWithFalseLeaveOpen(CompressionMode mode)
445445
{
446446
var ms = new MemoryStream();
447447
using var compressor = CreateStream(ms, mode);
448448
compressor.Dispose();
449449

450-
Assert.Null(BaseStream(compressor));
450+
Assert.Throws<ObjectDisposedException>(() => BaseStream(compressor));
451451

452452
compressor.Dispose(); // Should be a no-op
453453
}

src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,14 @@ private void ReleaseStateForDispose()
138138
/// <summary>Gets a reference to the underlying stream.</summary>
139139
/// <value>A stream object that represents the underlying stream.</value>
140140
/// <exception cref="System.ObjectDisposedException">The underlying stream is closed.</exception>
141-
public Stream BaseStream => _stream;
141+
public Stream BaseStream
142+
{
143+
get
144+
{
145+
EnsureNotDisposed();
146+
return _stream;
147+
}
148+
}
142149
/// <summary>Gets a value indicating whether the stream supports reading while decompressing a file.</summary>
143150
/// <value><see langword="true" /> if the <see cref="System.IO.Compression.CompressionMode" /> value is <see langword="Decompress," /> and the underlying stream supports reading and is not closed; otherwise, <see langword="false" />.</value>
144151
public override bool CanRead => _mode == CompressionMode.Decompress && _stream != null && _stream.CanRead;

src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,14 @@ private void EnsureBufferInitialized()
153153
}
154154
}
155155

156-
public Stream BaseStream => _stream;
156+
public Stream BaseStream
157+
{
158+
get
159+
{
160+
EnsureNotDisposed();
161+
return _stream;
162+
}
163+
}
157164

158165
public override bool CanRead
159166
{

src/libraries/System.IO.Compression/src/System/IO/Compression/GZipStream.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,14 @@ public override ValueTask DisposeAsync()
194194
return default;
195195
}
196196

197-
public Stream BaseStream => _deflateStream?.BaseStream!;
197+
public Stream BaseStream
198+
{
199+
get
200+
{
201+
CheckDeflateStream();
202+
return _deflateStream.BaseStream;
203+
}
204+
}
198205

199206
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
200207
{

src/libraries/System.IO.Compression/src/System/IO/Compression/ZLibStream.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,14 @@ public override ValueTask DisposeAsync()
286286
}
287287

288288
/// <summary>Gets a reference to the underlying stream.</summary>
289-
public Stream BaseStream => _deflateStream?.BaseStream!;
289+
public Stream BaseStream
290+
{
291+
get
292+
{
293+
ThrowIfClosed();
294+
return _deflateStream.BaseStream;
295+
}
296+
}
290297

291298
/// <summary>Throws an <see cref="ObjectDisposedException"/> if the stream is closed.</summary>
292299
private void ThrowIfClosed()

0 commit comments

Comments
 (0)