Skip to content

Commit 83b0d93

Browse files
mpidashstephentoubbuyaa-n
authored
Fix CA2022 warnings (Avoid inexact read with 'Stream.Read') (#100352)
* 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]>
1 parent 6561e7c commit 83b0d93

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

eng/CodeAnalysis.src.globalconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ dotnet_diagnostic.CA2020.severity = warning
561561
# CA2021: Do not call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types
562562
dotnet_diagnostic.CA2021.severity = warning
563563

564+
# CA2022: Avoid inexact read with 'Stream.Read'
565+
dotnet_diagnostic.CA2022.severity = warning
566+
564567
# CA2100: Review SQL queries for security vulnerabilities
565568
dotnet_diagnostic.CA2100.severity = none
566569

eng/CodeAnalysis.test.globalconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,9 @@ dotnet_diagnostic.CA2020.severity = none
558558
# CA2021: Do not call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types
559559
dotnet_diagnostic.CA2021.severity = none
560560

561+
# CA2022: Avoid inexact read with 'Stream.Read'
562+
dotnet_diagnostic.CA2022.severity = none
563+
561564
# CA2100: Review SQL queries for security vulnerabilities
562565
dotnet_diagnostic.CA2100.severity = none
563566

src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,21 @@ public string ReadExisting()
963963
Buffer.BlockCopy(_inBuffer, _readPos, bytesReceived, 0, CachedBytesToRead);
964964
}
965965

966-
_internalSerialStream.Read(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything
966+
#if NET7_0_OR_GREATER
967+
_internalSerialStream.ReadExactly(bytesReceived, CachedBytesToRead, bytesReceived.Length - CachedBytesToRead); // get everything
968+
#else
969+
int readCount = bytesReceived.Length - CachedBytesToRead;
970+
int totalRead = 0;
971+
while (totalRead < readCount)
972+
{
973+
int bytesRead = _internalSerialStream.Read(bytesReceived, CachedBytesToRead + totalRead, readCount - totalRead);
974+
if (bytesRead <= 0)
975+
{
976+
throw new EndOfStreamException();
977+
}
978+
totalRead += bytesRead;
979+
}
980+
#endif
967981

968982
// Read full characters and leave partial input in the buffer. Encoding.GetCharCount doesn't work because
969983
// it returns fallback characters on partial input, meaning that it overcounts. Instead, we use

src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,21 @@ public void Close()
8686
_bufferState = BufferState.Reading;
8787
_buffer = new byte[_stream.Length];
8888
_stream.Position = 0;
89-
_stream.Read(_buffer, 0, _buffer.Length);
89+
90+
#if NET7_0_OR_GREATER
91+
_stream.ReadExactly(_buffer);
92+
#else
93+
int totalRead = 0;
94+
while (totalRead < _buffer.Length)
95+
{
96+
int bytesRead = _stream.Read(_buffer, totalRead, _buffer.Length - totalRead);
97+
if (bytesRead <= 0)
98+
{
99+
throw new EndOfStreamException();
100+
}
101+
totalRead += bytesRead;
102+
}
103+
#endif
90104

91105
_writer = null;
92106
_stream = null;

src/libraries/System.Speech/src/Internal/Synthesis/AudioBase.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,22 @@ internal void PlayWaveFile(AudioData audio)
121121
try
122122
{
123123
byte[] data = new byte[(int)audio._stream.Length];
124-
audio._stream.Read(data, 0, data.Length);
124+
125+
#if NET7_0_OR_GREATER
126+
audio._stream.ReadExactly(data);
127+
#else
128+
int totalRead = 0;
129+
while (totalRead < data.Length)
130+
{
131+
int bytesRead = audio._stream.Read(data, totalRead, data.Length - totalRead);
132+
if (bytesRead <= 0)
133+
{
134+
throw new EndOfStreamException();
135+
}
136+
totalRead += bytesRead;
137+
}
138+
#endif
139+
125140
Play(data);
126141
}
127142
finally

src/libraries/System.Speech/src/Internal/Synthesis/EngineSite.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,22 @@ public Stream LoadResource(Uri uri, string mediaType)
174174
int cLen = (int)stream.Length;
175175
MemoryStream memStream = new(cLen);
176176
byte[] ab = new byte[cLen];
177-
stream.Read(ab, 0, ab.Length);
177+
178+
#if NET7_0_OR_GREATER
179+
stream.ReadExactly(ab);
180+
#else
181+
int totalRead = 0;
182+
while (totalRead < cLen)
183+
{
184+
int bytesRead = stream.Read(ab, totalRead, cLen - totalRead);
185+
if (bytesRead <= 0)
186+
{
187+
throw new EndOfStreamException();
188+
}
189+
totalRead += bytesRead;
190+
}
191+
#endif
192+
178193
_resourceLoader.UnloadFile(localPath);
179194
memStream.Write(ab, 0, cLen);
180195
memStream.Position = 0;

0 commit comments

Comments
 (0)