Skip to content

Commit c9a26cb

Browse files
authored
Remove unnecessary CA2022 suppressions (#15035)
* Remove unnecessary CA2022 suppressions * Remove unnecessary CA2022 suppressions * Fix char type * Format code * Format extension method
1 parent 0ba0e5b commit c9a26cb

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,9 @@ public bool TryGetDotnetPathByArchitecture(
409409
using var headerReader = _fileHelper.GetStream(path, FileMode.Open, FileAccess.Read);
410410
var magicBytes = new byte[4];
411411
var cpuInfoBytes = new byte[4];
412-
#pragma warning disable CA2022 // Avoid inexact read with 'Stream.Read'
413-
headerReader.Read(magicBytes, 0, magicBytes.Length);
414-
headerReader.Read(cpuInfoBytes, 0, cpuInfoBytes.Length);
415-
#pragma warning restore CA2022 // Avoid inexact read with 'Stream.Read'
412+
413+
ReadExactly(headerReader, magicBytes, 0, magicBytes.Length);
414+
ReadExactly(headerReader, cpuInfoBytes, 0, cpuInfoBytes.Length);
416415

417416
var magic = BitConverter.ToUInt32(magicBytes, 0);
418417
var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0);
@@ -435,6 +434,27 @@ public bool TryGetDotnetPathByArchitecture(
435434
return null;
436435
}
437436

437+
#if NET
438+
private static void ReadExactly(Stream stream, byte[] buffer, int offset, int count)
439+
{
440+
stream.ReadExactly(buffer, offset, count);
441+
}
442+
#else
443+
private static void ReadExactly(Stream stream, byte[] buffer, int offset, int count)
444+
{
445+
while (count > 0)
446+
{
447+
int read = stream.Read(buffer, offset, count);
448+
if (read <= 0)
449+
{
450+
throw new EndOfStreamException();
451+
}
452+
offset += read;
453+
count -= read;
454+
}
455+
}
456+
#endif
457+
438458
internal enum MacOsCpuType : uint
439459
{
440460
Arm64Magic = 0x0100000c,

0 commit comments

Comments
 (0)