diff --git a/Common.ruleset b/Common.ruleset index 147a92f576..ec6c561a34 100644 --- a/Common.ruleset +++ b/Common.ruleset @@ -147,13 +147,13 @@ - + - + diff --git a/src/BizHawk.Bizware.BizwareGL/math/Matrix4.cs b/src/BizHawk.Bizware.BizwareGL/math/Matrix4.cs index 633009e2bc..aedbf112f8 100644 --- a/src/BizHawk.Bizware.BizwareGL/math/Matrix4.cs +++ b/src/BizHawk.Bizware.BizwareGL/math/Matrix4.cs @@ -297,14 +297,16 @@ namespace BizHawk.Bizware.BizwareGL { readonly get { + if (rowIndex is < 0 or > 3) throw new ArgumentOutOfRangeException(paramName: nameof(rowIndex), rowIndex, message: "index out of range"); + if (columnIndex is < 0 or > 3) throw new ArgumentOutOfRangeException(paramName: nameof(columnIndex), columnIndex, message: "index out of range"); var i = rowIndex * 4 + columnIndex; - if (i < 0 || 15 < i) throw new IndexOutOfRangeException($"no such element m[{rowIndex}, {columnIndex}] of {nameof(Matrix4)}"); fixed (Matrix4* p = &this) return ((float*) p)[i]; } set { + if (rowIndex is < 0 or > 3) throw new ArgumentOutOfRangeException(paramName: nameof(rowIndex), rowIndex, message: "index out of range"); + if (columnIndex is < 0 or > 3) throw new ArgumentOutOfRangeException(paramName: nameof(columnIndex), columnIndex, message: "index out of range"); var i = rowIndex * 4 + columnIndex; - if (i < 0 || 15 < i) throw new IndexOutOfRangeException($"no such element m[{rowIndex}, {columnIndex}] of {nameof(Matrix4)}"); fixed (Matrix4* p = &this) ((float*) p)[i] = value; } } diff --git a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs index f2c1c47b0b..1a5376ae47 100644 --- a/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/CommonLibs/CommLuaLibrary.cs @@ -192,7 +192,7 @@ namespace BizHawk.Client.Common [return: LuaArbitraryStringParam] public string HttpTest() { - if (APIs.Comm.HTTP == null) throw new NullReferenceException(); // to match previous behaviour + _ = APIs.Comm.HTTP!; // to match previous behaviour return UnFixString(APIs.Comm.HttpTest()); } diff --git a/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs b/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs index e9965418ad..0d0695456f 100644 --- a/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs +++ b/src/BizHawk.Client.Common/rewind/ZwinderBuffer.cs @@ -286,8 +286,7 @@ namespace BizHawk.Client.Common /// public StateInformation GetState(int index) { - if ((uint)index >= (uint)Count) - throw new IndexOutOfRangeException(); + if ((uint) index >= (uint) Count) throw new ArgumentOutOfRangeException(paramName: nameof(index), index, message: "index out of range"); return new StateInformation(this, (index + _firstStateIndex) & STATEMASK); } @@ -297,8 +296,10 @@ namespace BizHawk.Client.Common /// public void InvalidateEnd(int index) { - if ((uint)index > (uint)Count) - throw new IndexOutOfRangeException(); + if ((uint) index > (uint) Count) // intentionally allows index == Count (e.g. clearing an empty buffer) + { + throw new ArgumentOutOfRangeException(paramName: nameof(index), index, message: "index out of range"); + } _nextStateIndex = (index + _firstStateIndex) & STATEMASK; //Util.DebugWriteLine($"Size: {Size >> 20}MiB, Used: {Used >> 20}MiB, States: {Count}"); } diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs index bf4d37a605..2d6a04fc56 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ConsoleLuaLibrary.cs @@ -70,7 +70,7 @@ namespace BizHawk.Client.EmuHawk var valueObjs = lti.Values; if (keyObjs.Count != valueObjs.Count) { - throw new IndexOutOfRangeException("each value must be paired with one key, they differ in number"); + throw new ArgumentException(message: "each value must be paired with one key, they differ in number", paramName: nameof(lti)); } var values = new object[keyObjs.Count]; diff --git a/src/BizHawk.Common/DeepEquality.cs b/src/BizHawk.Common/DeepEquality.cs index ec5919f3dc..c02de6f381 100644 --- a/src/BizHawk.Common/DeepEquality.cs +++ b/src/BizHawk.Common/DeepEquality.cs @@ -68,9 +68,7 @@ namespace BizHawk.Common return true; } - private static readonly MethodInfo ArrayEqualsGeneric - = typeof(DeepEquality).GetMethod(nameof(ArrayEquals), BindingFlags.NonPublic | BindingFlags.Static) - ?? throw new NullReferenceException(); + private static readonly MethodInfo ArrayEqualsGeneric = typeof(DeepEquality).GetMethod(nameof(ArrayEquals), BindingFlags.NonPublic | BindingFlags.Static)!; /// test if two objects and are equal, field-by-field (with deep inspection of each field) /// is an array with rank > 1 or is a non-zero-indexed array diff --git a/src/BizHawk.Common/HawkFile/HawkFile.cs b/src/BizHawk.Common/HawkFile/HawkFile.cs index 0876477ca9..33ffdabe4d 100644 --- a/src/BizHawk.Common/HawkFile/HawkFile.cs +++ b/src/BizHawk.Common/HawkFile/HawkFile.cs @@ -101,7 +101,7 @@ namespace BizHawk.Common _extractor = DearchivalMethod.Construct(path); try { - _archiveItems = _extractor.Scan() ?? throw new NullReferenceException(); + _archiveItems = _extractor.Scan()!; IsArchive = true; } catch diff --git a/src/BizHawk.Emulation.Common/Sound/SpeexResampler.cs b/src/BizHawk.Emulation.Common/Sound/SpeexResampler.cs index 01b10fd5e7..151450d0cf 100644 --- a/src/BizHawk.Emulation.Common/Sound/SpeexResampler.cs +++ b/src/BizHawk.Emulation.Common/Sound/SpeexResampler.cs @@ -66,8 +66,10 @@ namespace BizHawk.Emulation.Common { case LibSpeexDSP.RESAMPLER_ERR.SUCCESS: return; +#pragma warning disable MA0012 // this will crash the .NET host, as it should case LibSpeexDSP.RESAMPLER_ERR.ALLOC_FAILED: throw new InsufficientMemoryException($"{nameof(LibSpeexDSP)}: Alloc failed"); +#pragma warning restore MA0012 case LibSpeexDSP.RESAMPLER_ERR.BAD_STATE: throw new Exception($"{nameof(LibSpeexDSP)}: Bad state"); case LibSpeexDSP.RESAMPLER_ERR.INVALID_ARG: diff --git a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.cs b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.cs index 061f320b47..13dd422c4d 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Intellivision/Intellivision.cs @@ -87,7 +87,7 @@ namespace BizHawk.Emulation.Cores.Intellivision { if (erom.Length != 8192) { - throw new ApplicationException("EROM file is wrong size - expected 8192 bytes"); + throw new ArgumentException(message: "EROM file is wrong size - expected 8192 bytes", paramName: nameof(erom)); } int index = 0; @@ -103,7 +103,7 @@ namespace BizHawk.Emulation.Cores.Intellivision { if (grom.Length != 2048) { - throw new ApplicationException("GROM file is wrong size - expected 2048 bytes"); + throw new ArgumentException(message: "GROM file is wrong size - expected 2048 bytes", paramName: nameof(grom)); } GraphicsRom = grom; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IMemoryDomains.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IMemoryDomains.cs index b13b40298f..3510f10a34 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IMemoryDomains.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.IMemoryDomains.cs @@ -78,14 +78,11 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA _sram.Data = s.sram; // special combined ram memory domain + const int SIZE_COMBINED = (256 + 32) * 1024; _cwram.Peek = addr => { - if (addr < 0 || addr >= (256 + 32) * 1024) - { - throw new IndexOutOfRangeException(); - } - + if (addr is < 0 or >= SIZE_COMBINED) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "invalid address"); if (addr >= 256 * 1024) { return PeekWRAM(s.iwram, addr & 32767); @@ -96,11 +93,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA _cwram.Poke = (addr, val) => { - if (addr < 0 || addr >= (256 + 32) * 1024) - { - throw new IndexOutOfRangeException(); - } - + if (addr is < 0 or >= SIZE_COMBINED) throw new ArgumentOutOfRangeException(paramName: nameof(addr), addr, message: "invalid address"); if (addr >= 256 * 1024) { PokeWRAM(s.iwram, addr & 32767, val);