Skip to content

Commit

Permalink
Fix CA2022 warnings (Avoid inexact read with 'Stream.Read') (#100352)
Browse files Browse the repository at this point in the history
* Configure CA2022 severity

* Fix CA2022 warnings

* Check for NET7_0_OR_GREATER before using ReadExactly

* Fix CS1503

* Formatting

---------

Co-authored-by: Stephen Toub <[email protected]>
Co-authored-by: Buyaa Namnan <[email protected]>
  • Loading branch information
3 people authored Mar 29, 2024
1 parent 6561e7c commit 83b0d93
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
3 changes: 3 additions & 0 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ dotnet_diagnostic.CA2020.severity = warning
# CA2021: Do not call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types
dotnet_diagnostic.CA2021.severity = warning

# CA2022: Avoid inexact read with 'Stream.Read'
dotnet_diagnostic.CA2022.severity = warning

# CA2100: Review SQL queries for security vulnerabilities
dotnet_diagnostic.CA2100.severity = none

Expand Down
3 changes: 3 additions & 0 deletions eng/CodeAnalysis.test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,9 @@ dotnet_diagnostic.CA2020.severity = none
# CA2021: Do not call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types
dotnet_diagnostic.CA2021.severity = none

# CA2022: Avoid inexact read with 'Stream.Read'
dotnet_diagnostic.CA2022.severity = none

# CA2100: Review SQL queries for security vulnerabilities
dotnet_diagnostic.CA2100.severity = none

Expand Down
16 changes: 15 additions & 1 deletion src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,21 @@ public string ReadExisting()
Buffer.BlockCopy(_inBuffer, _readPos, bytesReceived, 0, CachedBytesToRead);
}

_internalSerialStream.Read(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything
#if NET7_0_OR_GREATER
_internalSerialStream.ReadExactly(bytesReceived, CachedBytesToRead, bytesReceived.Length - CachedBytesToRead); // get everything
#else
int readCount = bytesReceived.Length - CachedBytesToRead;
int totalRead = 0;
while (totalRead < readCount)
{
int bytesRead = _internalSerialStream.Read(bytesReceived, CachedBytesToRead + totalRead, readCount - totalRead);
if (bytesRead <= 0)
{
throw new EndOfStreamException();
}
totalRead += bytesRead;
}
#endif

// Read full characters and leave partial input in the buffer. Encoding.GetCharCount doesn't work because
// it returns fallback characters on partial input, meaning that it overcounts. Instead, we use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,21 @@ public void Close()
_bufferState = BufferState.Reading;
_buffer = new byte[_stream.Length];
_stream.Position = 0;
_stream.Read(_buffer, 0, _buffer.Length);

#if NET7_0_OR_GREATER
_stream.ReadExactly(_buffer);
#else
int totalRead = 0;
while (totalRead < _buffer.Length)
{
int bytesRead = _stream.Read(_buffer, totalRead, _buffer.Length - totalRead);
if (bytesRead <= 0)
{
throw new EndOfStreamException();
}
totalRead += bytesRead;
}
#endif

_writer = null;
_stream = null;
Expand Down
17 changes: 16 additions & 1 deletion src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,22 @@ internal void PlayWaveFile(AudioData audio)
try
{
byte[] data = new byte[(int)audio._stream.Length];
audio._stream.Read(data, 0, data.Length);

#if NET7_0_OR_GREATER
audio._stream.ReadExactly(data);
#else
int totalRead = 0;
while (totalRead < data.Length)
{
int bytesRead = audio._stream.Read(data, totalRead, data.Length - totalRead);
if (bytesRead <= 0)
{
throw new EndOfStreamException();
}
totalRead += bytesRead;
}
#endif

Play(data);
}
finally
Expand Down
17 changes: 16 additions & 1 deletion src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,22 @@ public Stream LoadResource(Uri uri, string mediaType)
int cLen = (int)stream.Length;
MemoryStream memStream = new(cLen);
byte[] ab = new byte[cLen];
stream.Read(ab, 0, ab.Length);

#if NET7_0_OR_GREATER
stream.ReadExactly(ab);
#else
int totalRead = 0;
while (totalRead < cLen)
{
int bytesRead = stream.Read(ab, totalRead, cLen - totalRead);
if (bytesRead <= 0)
{
throw new EndOfStreamException();
}
totalRead += bytesRead;
}
#endif

_resourceLoader.UnloadFile(localPath);
memStream.Write(ab, 0, cLen);
memStream.Position = 0;
Expand Down

0 comments on commit 83b0d93

Please sign in to comment.