Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit cc5d4cd

Browse files
committed
Fix up a few places in DeflateStream
Two changes: - Get rid of some dead code in DeflateStream - In debug builds, DeflateStream is asserting when the underlying stream returns bad data; in release it's throwing an exception. I've changed it to always throw; this will also help to avoid future AVs if we decide to switch any more of the implementation to unsafe code.
1 parent 7ea9cd5 commit cc5d4cd

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

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

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,32 @@ public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
4242
{
4343
if (stream == null)
4444
throw new ArgumentNullException("stream");
45-
46-
if (CompressionMode.Compress != mode && CompressionMode.Decompress != mode)
47-
throw new ArgumentException(SR.ArgumentOutOfRange_Enum, "mode");
48-
49-
_stream = stream;
50-
_mode = mode;
51-
_leaveOpen = leaveOpen;
52-
53-
switch (_mode)
45+
46+
switch (mode)
5447
{
5548
case CompressionMode.Decompress:
56-
57-
if (!_stream.CanRead)
49+
if (!stream.CanRead)
5850
{
5951
throw new ArgumentException(SR.NotReadableStream, "stream");
6052
}
61-
6253
_inflater = new Inflater();
63-
6454
break;
6555

6656
case CompressionMode.Compress:
67-
68-
if (!_stream.CanWrite)
57+
if (!stream.CanWrite)
6958
{
7059
throw new ArgumentException(SR.NotWriteableStream, "stream");
7160
}
72-
7361
_deflater = CreateDeflater(null);
74-
7562
break;
76-
} // switch (_mode)
7763

64+
default:
65+
throw new ArgumentException(SR.ArgumentOutOfRange_Enum, "mode");
66+
}
67+
68+
_stream = stream;
69+
_mode = mode;
70+
_leaveOpen = leaveOpen;
7871
_buffer = new byte[DefaultBufferSize];
7972
}
8073

@@ -120,22 +113,16 @@ private static IDeflater CreateDeflater(CompressionLevel? compressionLevel)
120113
deflatorType = s_forcedTestingDeflaterType;
121114
#endif
122115

123-
switch (deflatorType)
116+
if (deflatorType == WorkerType.ZLib)
124117
{
125-
case WorkerType.Managed:
126-
return new DeflaterManaged();
127-
128-
case WorkerType.ZLib:
129-
if (compressionLevel.HasValue)
130-
return new DeflaterZLib(compressionLevel.Value);
131-
else
132-
return new DeflaterZLib();
133-
134-
default:
135-
// We do not expect this to ever be thrown.
136-
// But this is better practice than returning null.
137-
Environment.FailFast("Program entered an unexpected state.");
138-
return null; // we'll not reach here
118+
return compressionLevel.HasValue ?
119+
new DeflaterZLib(compressionLevel.Value) :
120+
new DeflaterZLib();
121+
}
122+
else
123+
{
124+
Debug.Assert(deflatorType == WorkerType.Managed);
125+
return new DeflaterManaged();
139126
}
140127
}
141128

@@ -221,7 +208,6 @@ public override long Position
221208
public override void Flush()
222209
{
223210
EnsureNotDisposed();
224-
return;
225211
}
226212

227213
public override Task FlushAsync(CancellationToken cancellationToken)
@@ -273,9 +259,13 @@ public override int Read(byte[] array, int offset, int count)
273259
Debug.Assert(_inflater.NeedsInput(), "We can only run into this case if we are short of input");
274260

275261
int bytes = _stream.Read(_buffer, 0, _buffer.Length);
276-
if (bytes == 0)
262+
if (bytes <= 0)
277263
{
278-
break; //Do we want to throw an exception here?
264+
break;
265+
}
266+
else if (bytes > _buffer.Length)
267+
{
268+
throw new InvalidDataException(SR.GenericInvalidData);
279269
}
280270

281271
_inflater.SetInput(_buffer, 0, bytes);
@@ -386,6 +376,10 @@ private async Task<int> ReadAsyncCore(Task<int> readTask, byte[] array, int offs
386376
// This indicates the base stream has received EOF
387377
return 0;
388378
}
379+
else if (bytesRead > _buffer.Length)
380+
{
381+
throw new InvalidDataException(SR.GenericInvalidData);
382+
}
389383

390384
cancellationToken.ThrowIfCancellationRequested();
391385

0 commit comments

Comments
 (0)