diff --git a/eng/CodeAnalysis.src.globalconfig b/eng/CodeAnalysis.src.globalconfig index abdfba711bcea..21a53462cc5de 100644 --- a/eng/CodeAnalysis.src.globalconfig +++ b/eng/CodeAnalysis.src.globalconfig @@ -561,6 +561,9 @@ dotnet_diagnostic.CA2020.severity = warning # CA2021: Do not call Enumerable.Cast or Enumerable.OfType 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 diff --git a/eng/CodeAnalysis.test.globalconfig b/eng/CodeAnalysis.test.globalconfig index dccb23a9e1a8f..0d944fbd890fc 100644 --- a/eng/CodeAnalysis.test.globalconfig +++ b/eng/CodeAnalysis.test.globalconfig @@ -558,6 +558,9 @@ dotnet_diagnostic.CA2020.severity = none # CA2021: Do not call Enumerable.Cast or Enumerable.OfType 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 diff --git a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs index 83a9a02926bdd..752163fd2bd37 100644 --- a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs +++ b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs @@ -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 diff --git a/src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs b/src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs index f57dec531c28e..17f3cfa2e1e7a 100644 --- a/src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs +++ b/src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs @@ -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; diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs index ff57d09872075..782cd59fb6c34 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs @@ -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 diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs b/src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs index e37766fc656b9..a658f37ca6180 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs @@ -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;