From 53cdd1ddc877ca47d7a752fe05a6837e18b08f57 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Wed, 27 Mar 2024 18:13:03 +0100 Subject: [PATCH 1/5] Configure CA2022 severity --- eng/CodeAnalysis.src.globalconfig | 3 +++ eng/CodeAnalysis.test.globalconfig | 3 +++ 2 files changed, 6 insertions(+) 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 From d9ea36afd7311bb63e8e5cf9a5f659dfd00870c4 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Wed, 27 Mar 2024 18:13:17 +0100 Subject: [PATCH 2/5] Fix CA2022 warnings --- src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs | 2 +- .../src/System/ServiceModel/XmlBuffer.cs | 2 +- src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs | 2 +- .../System.Speech/src/Internal/Synthesis/EngineSite.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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..335af035c4c8a 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,7 @@ public string ReadExisting() Buffer.BlockCopy(_inBuffer, _readPos, bytesReceived, 0, CachedBytesToRead); } - _internalSerialStream.Read(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything + _internalSerialStream.ReadExactly(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything // 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..4cd2a1a9be2a5 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,7 @@ public void Close() _bufferState = BufferState.Reading; _buffer = new byte[_stream.Length]; _stream.Position = 0; - _stream.Read(_buffer, 0, _buffer.Length); + _stream.ReadExactly(_buffer); _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..daa0ea78e1c9f 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs @@ -121,7 +121,7 @@ internal void PlayWaveFile(AudioData audio) try { byte[] data = new byte[(int)audio._stream.Length]; - audio._stream.Read(data, 0, data.Length); + audio._stream.ReadExactly(data); 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..6d23850c6b00c 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs @@ -174,7 +174,7 @@ 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); + stream.ReadExactly(ab); _resourceLoader.UnloadFile(localPath); memStream.Write(ab, 0, cLen); memStream.Position = 0; From ba7d12ca8c707c09ff619c23d277f7229b817678 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Wed, 27 Mar 2024 19:21:36 +0100 Subject: [PATCH 3/5] Check for NET7_0_OR_GREATER before using ReadExactly --- .../src/System/IO/Ports/SerialPort.cs | 14 ++++++++++++++ .../src/System/ServiceModel/XmlBuffer.cs | 14 ++++++++++++++ .../src/Internal/Synthesis/AudioBase.cs | 15 +++++++++++++++ .../src/Internal/Synthesis/EngineSite.cs | 15 +++++++++++++++ 4 files changed, 58 insertions(+) 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 335af035c4c8a..c2f78e9d07b90 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); } +#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 4cd2a1a9be2a5..5a4de7ce8f926 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; + +#if NET7_0_OR_GREATER _stream.ReadExactly(_buffer); +#else + int totalRead = 0; + while (totalRead < _stream.Length) + { + int bytesRead = _stream.Read(_buffer, totalRead, _stream.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 daa0ea78e1c9f..be36bed95b8fc 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]; + +#if NET7_0_OR_GREATER audio._stream.ReadExactly(data); +#else + int totalRead = 0; + while (totalRead < audio._stream.Length) + { + int bytesRead = audio._stream.Read(data, totalRead, audio._stream.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 6d23850c6b00c..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]; + +#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; From b94df008a0fd7487f94637e2df127c02696bb1e7 Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Thu, 28 Mar 2024 00:35:56 +0100 Subject: [PATCH 4/5] Fix CS1503 --- .../src/System/ServiceModel/XmlBuffer.cs | 4 ++-- .../System.Speech/src/Internal/Synthesis/AudioBase.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 5a4de7ce8f926..17f3cfa2e1e7a 100644 --- a/src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs +++ b/src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs @@ -91,9 +91,9 @@ public void Close() _stream.ReadExactly(_buffer); #else int totalRead = 0; - while (totalRead < _stream.Length) + while (totalRead < _buffer.Length) { - int bytesRead = _stream.Read(_buffer, totalRead, _stream.Length - totalRead); + int bytesRead = _stream.Read(_buffer, totalRead, _buffer.Length - totalRead); if (bytesRead <= 0) { throw new EndOfStreamException(); diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs index be36bed95b8fc..782cd59fb6c34 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs @@ -126,9 +126,9 @@ internal void PlayWaveFile(AudioData audio) audio._stream.ReadExactly(data); #else int totalRead = 0; - while (totalRead < audio._stream.Length) + while (totalRead < data.Length) { - int bytesRead = audio._stream.Read(data, totalRead, audio._stream.Length - totalRead); + int bytesRead = audio._stream.Read(data, totalRead, data.Length - totalRead); if (bytesRead <= 0) { throw new EndOfStreamException(); From 79f13efe820dd570a3a26e3a9c5aade229aa696f Mon Sep 17 00:00:00 2001 From: Mario Pistrich Date: Thu, 28 Mar 2024 00:36:05 +0100 Subject: [PATCH 5/5] Formatting --- src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c2f78e9d07b90..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 @@ -964,7 +964,7 @@ public string ReadExisting() } #if NET7_0_OR_GREATER - _internalSerialStream.ReadExactly(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything + _internalSerialStream.ReadExactly(bytesReceived, CachedBytesToRead, bytesReceived.Length - CachedBytesToRead); // get everything #else int readCount = bytesReceived.Length - CachedBytesToRead; int totalRead = 0;