Fix CA2022 warnings (Avoid inexact read with 'Stream.Read')#100352
Fix CA2022 warnings (Avoid inexact read with 'Stream.Read')#100352buyaa-n merged 7 commits intodotnet:mainfrom
Conversation
| } | ||
|
|
||
| _internalSerialStream.Read(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything | ||
| _internalSerialStream.ReadExactly(bytesReceived, CachedBytesToRead, bytesReceived.Length - (CachedBytesToRead)); // get everything |
There was a problem hiding this comment.
This won't compile as written. This library builds for multiple target frameworks, some of which don't have ReadExactly.
There was a problem hiding this comment.
Thanks, I've added a check for NET7_0_OR_GREATER before using ReadExactly.
Is there a build target to check locally if I've used a function that is not available in a target framework? I ran the tests locally with .\build.cmd -subset clr+mono+libs+libs.tests -test -rc Release but got no build error.
There was a problem hiding this comment.
Thanks. That will only end up building for the current platform. There used to be a command for building all flavors of all libraries, but I can't find it in the docs now. @ViktorHofer? In the meantime, you can cd into the src folder for the library and dotnet build will build all targets for it.
There was a problem hiding this comment.
build.cmd libs -allconfigurations
| _buffer = new byte[_stream.Length]; | ||
| _stream.Position = 0; | ||
| _stream.Read(_buffer, 0, _buffer.Length); | ||
| _stream.ReadExactly(_buffer); |
| { | ||
| byte[] data = new byte[(int)audio._stream.Length]; | ||
| audio._stream.Read(data, 0, data.Length); | ||
| audio._stream.ReadExactly(data); |
| MemoryStream memStream = new(cLen); | ||
| byte[] ab = new byte[cLen]; | ||
| stream.Read(ab, 0, ab.Length); | ||
| stream.ReadExactly(ab); |
src/libraries/System.ServiceModel.Syndication/src/System/ServiceModel/XmlBuffer.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.cs
Outdated
Show resolved
Hide resolved
| while (totalRead < audio._stream.Length) | ||
| { | ||
| int bytesRead = audio._stream.Read(data, totalRead, audio._stream.Length - totalRead); |
There was a problem hiding this comment.
NIT:
| while (totalRead < audio._stream.Length) | |
| { | |
| int bytesRead = audio._stream.Read(data, totalRead, audio._stream.Length - totalRead); | |
| while (totalRead < data.Length) | |
| { | |
| int bytesRead = audio._stream.Read(data, totalRead, data.Length - totalRead); |
There was a problem hiding this comment.
Thanks, I will use data.Length here, but I am not sure why this does not trigger CS1503 as well.
Follow-up from dotnet/roslyn-analyzers#7208.
The following finding from the initial analyzer PR that flags an
UnmanagedMemoryStreamwas not fixed, as we excluded well known reliable stream types (MemoryStreamandUnmanagedMemoryStream, see dotnet/roslyn-analyzers#7268):runtime/src/libraries/System.Private.CoreLib/src/System/IO/UnmanagedMemoryStreamWrapper.cs
Line 93 in 79dd9ba