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
This commit is contained in:
YoshiRulz 2024-08-29 22:31:01 +10:00
parent 15ac8e2905
commit 10ebea4dda
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
6 changed files with 16 additions and 7 deletions

View File

@ -1,3 +1,11 @@
M:System.BitConverter.ToDouble(System.Byte[],System.Int32);use MemoryMarshal.{Try,}Read<double> (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<short> (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<int> (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<long> (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<float> (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<ushort> (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<uint> (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<ulong> (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

View File

@ -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<long>(sizeArr);
var sizeMask = reader.ReadInt64();
var targetFrameLength = reader.ReadInt32();
var useCompression = reader.ReadBoolean();

View File

@ -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<ulong>(ptr: optional_program_id, count: 1));
FirmwareID seeddbFWID = new("3DS", "seeddb");
using BinaryReader seeddb = new(GetFirmware(seeddbFWID));

View File

@ -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<int>(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;

View File

@ -5,7 +5,7 @@
<Import Project="../MainSlnCommon.props" />
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<NoWarn>$(NoWarn);BHI1104;CA1806;CA1825;CA2214;MA0060;MA0084;MA0090;MA0140;SA1100;SA1120;SA1129;SA1137;SA1205;SA1208;SA1400;SA1514;SA1517</NoWarn>
<NoWarn>$(NoWarn);BHI1104;CA1806;CA1825;CA2214;MA0060;MA0084;MA0090;MA0140;RS0030;SA1100;SA1120;SA1129;SA1137;SA1205;SA1208;SA1400;SA1514;SA1517</NoWarn>
<Nullable>disable</Nullable>
</PropertyGroup>
<ItemGroup>

View File

@ -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<ulong>(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());