From 10ebea4dda87cbc65dd3dd74cd83655b1a69dff1 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Thu, 29 Aug 2024 22:31:01 +1000 Subject: [PATCH] Ban `BitConverter.To*` I was hoping not to mute this Analyzer for the Cores project, but there were a couple usages of `BitConverter` there that were too far gone into dumb territory to fix in this commit --- src/BannedSymbols.BannedApiAnalyzers.txt | 8 ++++++++ src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs | 3 ++- .../RetroAchievements.GameVerification.cs | 4 +--- src/BizHawk.Common/Util.cs | 3 ++- .../BizHawk.Emulation.Cores.csproj | 2 +- .../DiscFormats/MDS_Format.cs | 3 ++- 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/BannedSymbols.BannedApiAnalyzers.txt b/src/BannedSymbols.BannedApiAnalyzers.txt index ccd0431015..d29b798d2d 100644 --- a/src/BannedSymbols.BannedApiAnalyzers.txt +++ b/src/BannedSymbols.BannedApiAnalyzers.txt @@ -1,3 +1,11 @@ +M:System.BitConverter.ToDouble(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from an 8-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToInt16(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from a 2-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToInt32(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from a 4-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToInt64(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from an 8-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToSingle(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from a 4-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToUInt16(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from a 2-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToUInt32(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from a 4-octet struct, reinterpret-cast it with Unsafe.As instead +M:System.BitConverter.ToUInt64(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read (implicit/host endianness; for explicit there's BinaryPrimitives), or if the byte array was made from an 8-octet struct, reinterpret-cast it with Unsafe.As instead M:System.Convert.ToByte(System.String);use byte.{Try,}Parse M:System.Convert.ToDecimal(System.String);use decimal.{Try,}Parse M:System.Convert.ToDouble(System.String);use double.{Try,}Parse diff --git a/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs b/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs index c98286fbd0..1144385211 100644 --- a/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs +++ b/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using BizHawk.Common; @@ -353,7 +354,7 @@ namespace BizHawk.Client.Common { byte[] sizeArr = new byte[8]; reader.Read(sizeArr, 1, 7); - var size = BitConverter.ToInt64(sizeArr, 0); + var size = MemoryMarshal.Read(sizeArr); var sizeMask = reader.ReadInt64(); var targetFrameLength = reader.ReadInt32(); var useCompression = reader.ReadBoolean(); diff --git a/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs b/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs index 21d9afbb97..7155fb10b4 100644 --- a/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs +++ b/src/BizHawk.Client.EmuHawk/RetroAchievements/RetroAchievements.GameVerification.cs @@ -429,9 +429,7 @@ namespace BizHawk.Client.EmuHawk return true; } - var programIdBytes = new byte[8]; - Marshal.Copy(optional_program_id, programIdBytes, 0, 8); - var programId = BitConverter.ToUInt64(programIdBytes, 0); + var programId = MemoryMarshal.GetReference(Util.UnsafeSpanFromPointer(ptr: optional_program_id, count: 1)); FirmwareID seeddbFWID = new("3DS", "seeddb"); using BinaryReader seeddb = new(GetFirmware(seeddbFWID)); diff --git a/src/BizHawk.Common/Util.cs b/src/BizHawk.Common/Util.cs index 7a74dfd646..019ec466ab 100644 --- a/src/BizHawk.Common/Util.cs +++ b/src/BizHawk.Common/Util.cs @@ -5,6 +5,7 @@ using System.IO.Compression; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; namespace BizHawk.Common @@ -57,7 +58,7 @@ namespace BizHawk.Common Debug.Assert(bytesRead == tmp.Length, "failed to read tail"); src.Seek(0, SeekOrigin.Begin); using var gs = new GZipStream(src, CompressionMode.Decompress, true); - var data = new byte[BitConverter.ToInt32(tmp, 0)]; + var data = new byte[MemoryMarshal.Read(tmp)]; //TODO definitely not a uint? worth checking, though values >= 0x80000000U would immediately throw here since it would amount to a negative array length using var ms = new MemoryStream(data); gs.CopyTo(ms); return data; diff --git a/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj b/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj index d7b08cadde..1865bbc157 100644 --- a/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj +++ b/src/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj @@ -5,7 +5,7 @@ true - $(NoWarn);BHI1104;CA1806;CA1825;CA2214;MA0060;MA0084;MA0090;MA0140;SA1100;SA1120;SA1129;SA1137;SA1205;SA1208;SA1400;SA1514;SA1517 + $(NoWarn);BHI1104;CA1806;CA1825;CA2214;MA0060;MA0084;MA0090;MA0140;RS0030;SA1100;SA1120;SA1129;SA1137;SA1205;SA1208;SA1400;SA1514;SA1517 disable diff --git a/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs b/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs index 1926e36ba3..1b35f404f6 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscFormats/MDS_Format.cs @@ -3,6 +3,7 @@ using System.IO; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Runtime.InteropServices; using BizHawk.Common.PathExtensions; @@ -394,7 +395,7 @@ namespace BizHawk.Emulation.DiscSystem track.ExtraOffset = bc.ToInt32(trackHeader.Skip(12).Take(4).ToArray()); track.SectorSize = bc.ToInt16(trackHeader.Skip(16).Take(2).ToArray()); track.PLBA = bc.ToInt32(trackHeader.Skip(36).Take(4).ToArray()); - track.StartOffset = BitConverter.ToUInt64(trackHeader.Skip(40).Take(8).ToArray(), 0); + track.StartOffset = MemoryMarshal.Read(trackHeader.AsSpan(start: 12 + sizeof(int) + sizeof(short) + 18 + sizeof(int))); track.Files = bc.ToInt32(trackHeader.Skip(48).Take(4).ToArray()); track.FooterOffset = bc.ToInt32(trackHeader.Skip(52).Take(4).ToArray());