From 64050f594ad5bf5fb644df568676b09570da087e Mon Sep 17 00:00:00 2001 From: Winniexu01 Date: Wed, 26 Mar 2025 08:41:53 +0000 Subject: [PATCH 1/5] Remove unnecessary CA2022 suppressions --- .../Helpers/DotnetHostHelper.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs index 0d10e0dee8..519ee34410 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs @@ -409,10 +409,8 @@ public bool TryGetDotnetPathByArchitecture( using var headerReader = _fileHelper.GetStream(path, FileMode.Open, FileAccess.Read); var magicBytes = new byte[4]; var cpuInfoBytes = new byte[4]; -#pragma warning disable CA2022 // Avoid inexact read with 'Stream.Read' headerReader.Read(magicBytes, 0, magicBytes.Length); headerReader.Read(cpuInfoBytes, 0, cpuInfoBytes.Length); -#pragma warning restore CA2022 // Avoid inexact read with 'Stream.Read' var magic = BitConverter.ToUInt32(magicBytes, 0); var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0); From 8b0553b9ec628ec987efc3a3032d85c34b8ddf57 Mon Sep 17 00:00:00 2001 From: Winniexu01 Date: Fri, 28 Mar 2025 08:20:07 +0000 Subject: [PATCH 2/5] Remove unnecessary CA2022 suppressions --- .../Helpers/DotnetHostHelper.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs index 519ee34410..e71b0efe28 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs @@ -409,8 +409,14 @@ public bool TryGetDotnetPathByArchitecture( using var headerReader = _fileHelper.GetStream(path, FileMode.Open, FileAccess.Read); var magicBytes = new byte[4]; var cpuInfoBytes = new byte[4]; - headerReader.Read(magicBytes, 0, magicBytes.Length); - headerReader.Read(cpuInfoBytes, 0, cpuInfoBytes.Length); + +#if NET + headerReader.ReadExactly(magicBytes, 0, magicBytes.Length); + headerReader.ReadExactly(cpuInfoBytes, 0, cpuInfoBytes.Length); +#else + ReadExist(headerReader, magicBytes, 0, magicBytes.Length); + ReadExist(headerReader, cpuInfoBytes, 0, cpuInfoBytes.Length); +#endif var magic = BitConverter.ToUInt32(magicBytes, 0); var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0); @@ -433,6 +439,20 @@ public bool TryGetDotnetPathByArchitecture( return null; } + private static void ReadExist(FileStream stream, byte[] buffer, int offset, int count) + { + while (count > 0) + { + int read = stream.Read(buffer, offset, count); + if (read <= 0) + { + throw new EndOfStreamException(); + } + offset += read; + count -= read; + } + } + internal enum MacOsCpuType : uint { Arm64Magic = 0x0100000c, From 8115f367c2aed4c287c318a3eebe3ccff4ace354 Mon Sep 17 00:00:00 2001 From: Winniexu01 Date: Fri, 28 Mar 2025 08:43:22 +0000 Subject: [PATCH 3/5] Fix char type --- .../Helpers/DotnetHostHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs index e71b0efe28..7bb8308780 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs @@ -439,7 +439,7 @@ public bool TryGetDotnetPathByArchitecture( return null; } - private static void ReadExist(FileStream stream, byte[] buffer, int offset, int count) + private static void ReadExist(Stream stream, byte[] buffer, int offset, int count) { while (count > 0) { From ce5b8e188fcbe3491c066db2b8eba0f170e5cc2f Mon Sep 17 00:00:00 2001 From: Winniexu01 Date: Mon, 31 Mar 2025 01:43:40 +0000 Subject: [PATCH 4/5] Format code --- .../Helpers/DotnetHostHelper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs index 7bb8308780..b64adf9b9d 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs @@ -439,6 +439,7 @@ public bool TryGetDotnetPathByArchitecture( return null; } +#if !NET private static void ReadExist(Stream stream, byte[] buffer, int offset, int count) { while (count > 0) @@ -452,6 +453,7 @@ private static void ReadExist(Stream stream, byte[] buffer, int offset, int coun count -= read; } } +#endif internal enum MacOsCpuType : uint { From a6e372c0957cbbba36a92f95472c7b9e64017f3b Mon Sep 17 00:00:00 2001 From: Winniexu01 Date: Mon, 31 Mar 2025 07:09:04 +0000 Subject: [PATCH 5/5] Format extension method --- .../Helpers/DotnetHostHelper.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs index b64adf9b9d..d71c2f24d5 100644 --- a/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs +++ b/src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs @@ -410,13 +410,8 @@ public bool TryGetDotnetPathByArchitecture( var magicBytes = new byte[4]; var cpuInfoBytes = new byte[4]; -#if NET - headerReader.ReadExactly(magicBytes, 0, magicBytes.Length); - headerReader.ReadExactly(cpuInfoBytes, 0, cpuInfoBytes.Length); -#else - ReadExist(headerReader, magicBytes, 0, magicBytes.Length); - ReadExist(headerReader, cpuInfoBytes, 0, cpuInfoBytes.Length); -#endif + ReadExactly(headerReader, magicBytes, 0, magicBytes.Length); + ReadExactly(headerReader, cpuInfoBytes, 0, cpuInfoBytes.Length); var magic = BitConverter.ToUInt32(magicBytes, 0); var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0); @@ -439,8 +434,13 @@ public bool TryGetDotnetPathByArchitecture( return null; } -#if !NET - private static void ReadExist(Stream stream, byte[] buffer, int offset, int count) +#if NET + private static void ReadExactly(Stream stream, byte[] buffer, int offset, int count) + { + stream.ReadExactly(buffer, offset, count); + } +#else + private static void ReadExactly(Stream stream, byte[] buffer, int offset, int count) { while (count > 0) {