Ban runtime asserts without a message

This commit is contained in:
YoshiRulz 2024-08-26 01:24:01 +10:00
parent 8e12e44411
commit 67f87b15c4
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
19 changed files with 32 additions and 30 deletions

View File

@ -9,4 +9,6 @@ M:System.Convert.ToSingle(System.String);use float.{Try,}Parse
M:System.Convert.ToUInt16(System.String);use ushort.{Try,}Parse M:System.Convert.ToUInt16(System.String);use ushort.{Try,}Parse
M:System.Convert.ToUInt32(System.String);use uint.{Try,}Parse M:System.Convert.ToUInt32(System.String);use uint.{Try,}Parse
M:System.Convert.ToUInt64(System.String);use ulong.{Try,}Parse M:System.Convert.ToUInt64(System.String);use ulong.{Try,}Parse
M:System.Diagnostics.Debug.Assert(System.Boolean);include a unique message
M:System.Diagnostics.Trace.Assert(System.Boolean);include a unique message
M:System.Windows.Forms.Control.Focus;use Activate for Forms, or Select otherwise M:System.Windows.Forms.Control.Focus;use Activate for Forms, or Select otherwise

View File

@ -65,7 +65,7 @@ namespace BizHawk.Bizware.Graphics
public void UnlockBits(BitmapData bmpd) public void UnlockBits(BitmapData bmpd)
{ {
Debug.Assert(CurrLock == bmpd); Debug.Assert(CurrLock == bmpd, "must pass in the same object obtained from " + nameof(LockBits));
if (WrappedBitmap != null) if (WrappedBitmap != null)
{ {

View File

@ -285,7 +285,7 @@ namespace BizHawk.Client.Common.FilterManager
if (ps.Type == ProgramStepType.NewTarget) if (ps.Type == ProgramStepType.NewTarget)
{ {
var size = (Size)ps.Args; var size = (Size)ps.Args;
Debug.Assert(size == outsize); Debug.Assert(size == outsize, "size of last " + nameof(ProgramStepType.NewTarget) + " was set wrong");
Program[i] = new(ProgramStepType.FinalTarget, size, ps.Comment); Program[i] = new(ProgramStepType.FinalTarget, size, ps.Comment);
break; break;
} }

View File

@ -1,7 +1,6 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -27,7 +26,7 @@ namespace BizHawk.Client.Common
{ {
var @base = File.ReadAllBytes(baseFilename); var @base = File.ReadAllBytes(baseFilename);
var (patched, actualHash) = PerformPatchInMemory(@base, in patchOption); var (patched, actualHash) = PerformPatchInMemory(@base, in patchOption);
Trace.Assert(actualHash == patchOption.TargetHash); if (actualHash != patchOption.TargetHash) throw new InvalidOperationException("patch produced incorrect output");
var patchedParentDir = Path.Combine(pathEntries[PathEntryCollection.GLOBAL, "Temp Files"].Path, "AutopatchedFirmware"); var patchedParentDir = Path.Combine(pathEntries[PathEntryCollection.GLOBAL, "Temp Files"].Path, "AutopatchedFirmware");
Directory.CreateDirectory(patchedParentDir); Directory.CreateDirectory(patchedParentDir);
var ff = FirmwareDatabase.FirmwareFilesByHash[patchOption.TargetHash]; var ff = FirmwareDatabase.FirmwareFilesByHash[patchOption.TargetHash];

View File

@ -821,13 +821,13 @@ namespace BizHawk.Client.EmuHawk
private bool IsVisible(int index) private bool IsVisible(int index)
{ {
Debug.Assert(FirstVisibleRow < LastFullyVisibleRow); Debug.Assert(FirstVisibleRow < LastFullyVisibleRow, "rows out of order?");
return FirstVisibleRow <= index && index <= LastFullyVisibleRow; return FirstVisibleRow <= index && index <= LastFullyVisibleRow;
} }
public bool IsPartiallyVisible(int index) public bool IsPartiallyVisible(int index)
{ {
Debug.Assert(FirstVisibleRow < LastVisibleRow); Debug.Assert(FirstVisibleRow < LastVisibleRow, "rows out of order?");
return FirstVisibleRow <= index && index <= LastVisibleRow; return FirstVisibleRow <= index && index <= LastVisibleRow;
} }

View File

@ -110,7 +110,7 @@ namespace BizHawk.Client.EmuHawk
return; return;
} }
Debug.Assert(inFinalTarget); Debug.Assert(inFinalTarget, "not in final target?");
// present and conclude drawing // present and conclude drawing
_graphicsControl.SwapBuffers(); _graphicsControl.SwapBuffers();

View File

@ -24,6 +24,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Windows.Forms; using System.Windows.Forms;
@ -459,7 +460,7 @@ namespace BizHawk.Client.EmuHawk
dims.Height = dims.Width = Math.Max(dims.Width, dims.Height); dims.Height = dims.Width = Math.Max(dims.Width, dims.Height);
allocate(dims.Width, dims.Height); allocate(dims.Width, dims.Height);
numPixels = dims.Width * dims.Height; numPixels = dims.Width * dims.Height;
System.Diagnostics.Debug.Assert(stride / 4 == dims.Width); Debug.Assert(dims.Width * 4 == stride, "line is not `width` pixels at 32bpp?");
map = gd.FetchTilemap(bg.ScreenAddr, bg.ScreenSize); map = gd.FetchTilemap(bg.ScreenAddr, bg.ScreenSize);
int paletteStart = 0; int paletteStart = 0;

View File

@ -148,7 +148,7 @@ namespace BizHawk.Common
if (size is 0) if (size is 0)
{ {
var rleSize = (data[i++] * 0x100) | data[i++]; var rleSize = (data[i++] * 0x100) | data[i++];
Debug.Assert(rleSize is not 0); Debug.Assert(rleSize is not 0, "may not run-length-encode nothing");
records.Add((i, targetOffset, rleSize, true)); records.Add((i, targetOffset, rleSize, true));
i++; i++;
} }

View File

@ -14,19 +14,19 @@ namespace BizHawk.Common
public static implicit operator Bit(int rhs) public static implicit operator Bit(int rhs)
{ {
Debug.Assert((rhs & ~1) == 0); Debug.Assert((rhs & ~1) is 0, "higher bits can't be used");
return new Bit((uint)rhs); return new Bit((uint)rhs);
} }
public static implicit operator Bit(uint rhs) public static implicit operator Bit(uint rhs)
{ {
Debug.Assert((rhs & ~1) == 0); Debug.Assert((rhs & ~1) is 0, "higher bits can't be used");
return new Bit(rhs); return new Bit(rhs);
} }
public static implicit operator Bit(byte rhs) public static implicit operator Bit(byte rhs)
{ {
Debug.Assert((rhs & ~1) == 0); Debug.Assert((rhs & ~1) is 0, "higher bits can't be used");
return new Bit(rhs); return new Bit(rhs);
} }

View File

@ -9,7 +9,7 @@ namespace BizHawk.Common
{ {
#if true //TODO benchmark (both methods work correctly); also there is another method involving shifting the (output copy of the) array over by 1 byte then manually writing every second byte #if true //TODO benchmark (both methods work correctly); also there is another method involving shifting the (output copy of the) array over by 1 byte then manually writing every second byte
var l = a.Length; var l = a.Length;
Debug.Assert(l % 2 == 0); Debug.Assert(l % 2 is 0, "octets must be in pairs");
fixed (byte* p = &a[0]) for (var i = 0; i < l; i += 2) fixed (byte* p = &a[0]) for (var i = 0; i < l; i += 2)
{ {
var b = p[i]; var b = p[i];
@ -17,7 +17,7 @@ namespace BizHawk.Common
p[i + 1] = b; p[i + 1] = b;
} }
#else #else
Debug.Assert(a.Length % 2 == 0); Debug.Assert(a.Length % 2 is 0, "octets must be in pairs");
var shorts = MemoryMarshal.Cast<byte, ushort>(a); var shorts = MemoryMarshal.Cast<byte, ushort>(a);
for (var i = 0; i < shorts.Length; i++) shorts[i] = BinaryPrimitives.ReverseEndianness(shorts[i]); for (var i = 0; i < shorts.Length; i++) shorts[i] = BinaryPrimitives.ReverseEndianness(shorts[i]);
#endif #endif
@ -28,7 +28,7 @@ namespace BizHawk.Common
{ {
#if true //TODO benchmark (both methods work correctly) #if true //TODO benchmark (both methods work correctly)
var l = a.Length; var l = a.Length;
Debug.Assert(l % 4 == 0); Debug.Assert(l % 4 is 0, "octets must be in groups of 4");
fixed (byte* p = &a[0]) for (var i = 0; i < l; i += 4) fixed (byte* p = &a[0]) for (var i = 0; i < l; i += 4)
{ {
var b = p[i]; var b = p[i];
@ -39,7 +39,7 @@ namespace BizHawk.Common
p[i + 2] = b; p[i + 2] = b;
} }
#else #else
Debug.Assert(a.Length % 4 == 0); Debug.Assert(a.Length % 4 is 0, "octets must be in groups of 4");
var ints = MemoryMarshal.Cast<byte, uint>(a); var ints = MemoryMarshal.Cast<byte, uint>(a);
for (var i = 0; i < ints.Length; i++) ints[i] = BinaryPrimitives.ReverseEndianness(ints[i]); for (var i = 0; i < ints.Length; i++) ints[i] = BinaryPrimitives.ReverseEndianness(ints[i]);
#endif #endif

View File

@ -133,7 +133,7 @@ namespace BizHawk.Common.CollectionExtensions
if (a.Length is 0) return b; if (a.Length is 0) return b;
var combined = new T[a.Length + b.Length]; var combined = new T[a.Length + b.Length];
var returned = ((ReadOnlySpan<T>) a).ConcatArray(b, combined); var returned = ((ReadOnlySpan<T>) a).ConcatArray(b, combined);
Debug.Assert(returned == combined); Debug.Assert(returned == combined, "expecting return value to cover all of combined since the whole thing was written to");
return combined; return combined;
} }

View File

@ -54,7 +54,7 @@ namespace BizHawk.Common
if (tmp[0] != 0x1F || tmp[1] != 0x8B) throw new InvalidOperationException("GZIP header not present"); if (tmp[0] != 0x1F || tmp[1] != 0x8B) throw new InvalidOperationException("GZIP header not present");
src.Seek(-4, SeekOrigin.End); src.Seek(-4, SeekOrigin.End);
var bytesRead = src.Read(tmp, offset: 0, count: tmp.Length); var bytesRead = src.Read(tmp, offset: 0, count: tmp.Length);
Debug.Assert(bytesRead == tmp.Length); Debug.Assert(bytesRead == tmp.Length, "failed to read tail");
src.Seek(0, SeekOrigin.Begin); src.Seek(0, SeekOrigin.Begin);
using var gs = new GZipStream(src, CompressionMode.Decompress, true); using var gs = new GZipStream(src, CompressionMode.Decompress, true);
var data = new byte[BitConverter.ToInt32(tmp, 0)]; var data = new byte[BitConverter.ToInt32(tmp, 0)];

View File

@ -31,7 +31,7 @@ namespace BizHawk.Common
if (_md5Impl == null) if (_md5Impl == null)
{ {
_md5Impl = MD5.Create(); _md5Impl = MD5.Create();
Debug.Assert(_md5Impl.CanReuseTransform && _md5Impl.HashSize is EXPECTED_LENGTH); Debug.Assert(_md5Impl.CanReuseTransform && _md5Impl.HashSize is EXPECTED_LENGTH, "nonstandard implementation?");
} }
return _md5Impl; return _md5Impl;
} }

View File

@ -49,7 +49,7 @@ namespace BizHawk.Common
if (_sha1Impl == null) if (_sha1Impl == null)
{ {
_sha1Impl = SHA1.Create(); _sha1Impl = SHA1.Create();
Debug.Assert(_sha1Impl.CanReuseTransform && _sha1Impl.HashSize is EXPECTED_LENGTH); Debug.Assert(_sha1Impl.CanReuseTransform && _sha1Impl.HashSize is EXPECTED_LENGTH, "nonstandard implementation?");
} }
return _sha1Impl; return _sha1Impl;
} }

View File

@ -31,7 +31,7 @@ namespace BizHawk.Common
if (_sha256Impl == null) if (_sha256Impl == null)
{ {
_sha256Impl = SHA256.Create(); _sha256Impl = SHA256.Create();
Debug.Assert(_sha256Impl.CanReuseTransform && _sha256Impl.HashSize is EXPECTED_LENGTH); Debug.Assert(_sha256Impl.CanReuseTransform && _sha256Impl.HashSize is EXPECTED_LENGTH, "nonstandard implementation?");
} }
return _sha256Impl; return _sha256Impl;
} }

View File

@ -276,7 +276,7 @@ namespace BizHawk.Emulation.Common
private void RemoveAtInternal(int index) private void RemoveAtInternal(int index)
{ {
Debug.Assert(_modifyInProgress); Debug.Assert(_modifyInProgress, "unexpected collection mutation state");
CopyIfRequired(); CopyIfRequired();
var removedItem = _items[index]; var removedItem = _items[index];
@ -357,7 +357,7 @@ namespace BizHawk.Emulation.Common
private void EndCopyOnWrite() private void EndCopyOnWrite()
{ {
_copyOnWriteRequired--; _copyOnWriteRequired--;
Debug.Assert(_copyOnWriteRequired >= 0); Debug.Assert(_copyOnWriteRequired >= 0, "unexpected CoW state");
} }
public Enumerator GetEnumerator() public Enumerator GetEnumerator()

View File

@ -49,7 +49,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
{ {
// target number of samples to emit: length of 1 frame minus whatever overflow // target number of samples to emit: length of 1 frame minus whatever overflow
samplesEmitted = TICKSINFRAME - frameOverflow; samplesEmitted = TICKSINFRAME - frameOverflow;
Debug.Assert(samplesEmitted * 2 <= _soundbuff.Length); Debug.Assert(samplesEmitted * 2 <= _soundbuff.Length, "buffer capacity exceeded");
if (LibGambatte.gambatte_runfor(GambatteState, FrameBuffer, 160, _soundbuff, ref samplesEmitted) > 0) if (LibGambatte.gambatte_runfor(GambatteState, FrameBuffer, 160, _soundbuff, ref samplesEmitted) > 0)
{ {
Array.Copy(FrameBuffer, VideoBuffer, FrameBuffer.Length); Array.Copy(FrameBuffer, VideoBuffer, FrameBuffer.Length);
@ -90,7 +90,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy
inputFrameLengthInt = TICKSINFRAME; inputFrameLengthInt = TICKSINFRAME;
} }
samplesEmitted = inputFrameLengthInt - frameOverflow; samplesEmitted = inputFrameLengthInt - frameOverflow;
Debug.Assert(samplesEmitted * 2 <= _soundbuff.Length); Debug.Assert(samplesEmitted * 2 <= _soundbuff.Length, "buffer capacity exceeded");
if (LibGambatte.gambatte_runfor(GambatteState, FrameBuffer, 160, _soundbuff, ref samplesEmitted) > 0) if (LibGambatte.gambatte_runfor(GambatteState, FrameBuffer, 160, _soundbuff, ref samplesEmitted) > 0)
{ {
Array.Copy(FrameBuffer, VideoBuffer, FrameBuffer.Length); Array.Copy(FrameBuffer, VideoBuffer, FrameBuffer.Length);

View File

@ -5,6 +5,7 @@
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using BizHawk.Common.CollectionExtensions; using BizHawk.Common.CollectionExtensions;
using BizHawk.Common.NumberExtensions; using BizHawk.Common.NumberExtensions;
@ -171,7 +172,7 @@ namespace BizHawk.Emulation.DiscSystem
//Console.WriteLine("binary searched"); //use this to check for mistaken LastReadIndex logic resulting in binary searches during sequential access //Console.WriteLine("binary searched"); //use this to check for mistaken LastReadIndex logic resulting in binary searches during sequential access
var listIndex = Index.LowerBoundBinarySearch(idx => idx.LogicalOffset, offset); var listIndex = Index.LowerBoundBinarySearch(idx => idx.LogicalOffset, offset);
System.Diagnostics.Debug.Assert(listIndex < Index.Count); Debug.Assert(listIndex < Index.Count, "insertion point may not be after end");
//Console.WriteLine("byte_pos {0:X8} using index #{1} at offset {2:X8}", offset, listIndex, Index[listIndex].LogicalOffset); //Console.WriteLine("byte_pos {0:X8} using index #{1} at offset {2:X8}", offset, listIndex, Index[listIndex].LogicalOffset);
return listIndex; return listIndex;

View File

@ -81,10 +81,9 @@ namespace BizHawk.Emulation.DiscSystem
// plaindisc.ReadLBA_2352_Flat(item.Item1, chunkbuf_corlet, 0, item.Item2); // API has changed // plaindisc.ReadLBA_2352_Flat(item.Item1, chunkbuf_corlet, 0, item.Item2); // API has changed
// ecmdisc.ReadLBA_2352_Flat(item.Item1, chunkbuf_mine, 0, item.Item2); // API has changed // ecmdisc.ReadLBA_2352_Flat(item.Item1, chunkbuf_mine, 0, item.Item2); // API has changed
for (int i = 0; i < item.Item2; i++) for (int i = 0; i < item.Item2; i++)
if (chunkbuf_corlet[i] != chunkbuf_mine[i]) {
{ Debug.Assert(chunkbuf_corlet[i] == chunkbuf_mine[i], $"buffers differ at [{t}; {i}]");
Debug.Assert(false); }
}
} }
} }
} }