Enable MA0060 and fix noncompliance (except in Cores)

"The value returned by Stream.Read/Stream.ReadAsync is not used"
This commit is contained in:
YoshiRulz 2024-07-03 23:37:43 +10:00
parent 6bb60c1b76
commit a7e1c8fd09
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
14 changed files with 49 additions and 28 deletions

View File

@ -219,7 +219,7 @@ dotnet_diagnostic.MA0058.severity = error
# Class name should end with 'EventArgs' # Class name should end with 'EventArgs'
dotnet_diagnostic.MA0059.severity = silent dotnet_diagnostic.MA0059.severity = silent
# The value returned by Stream.Read/Stream.ReadAsync is not used # The value returned by Stream.Read/Stream.ReadAsync is not used
dotnet_diagnostic.MA0060.severity = silent dotnet_diagnostic.MA0060.severity = error
# Method overrides should not change parameter defaults # Method overrides should not change parameter defaults
dotnet_diagnostic.MA0061.severity = silent dotnet_diagnostic.MA0061.severity = silent
# Non-flags enums should not be marked with "FlagsAttribute" # Non-flags enums should not be marked with "FlagsAttribute"

View File

@ -4,6 +4,7 @@ using System.Linq;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Common; using BizHawk.Common;
using BizHawk.Common.IOExtensions;
using Silk.NET.Core.Native; using Silk.NET.Core.Native;
using Silk.NET.OpenAL; using Silk.NET.OpenAL;
@ -208,9 +209,7 @@ namespace BizHawk.Bizware.Audio
_wavSourceID = _al.GenSource(); _wavSourceID = _al.GenSource();
_wavBufferID = _al.GenBuffer(); _wavBufferID = _al.GenBuffer();
var tempBuffer = new byte[wavStream.Length]; var tempBuffer = wavStream.ReadAllBytes();
wavStream.Read(tempBuffer);
if (wavStream.Format == SDL2WavStream.AudioFormat.S16MSB) if (wavStream.Format == SDL2WavStream.AudioFormat.S16MSB)
{ {
EndiannessUtils.MutatingByteSwap16(tempBuffer); EndiannessUtils.MutatingByteSwap16(tempBuffer);

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -178,11 +179,12 @@ namespace BizHawk.Bizware.Audio
StopWav(); StopWav();
_wavVoice = _device.CreateSourceVoice(format); _wavVoice = _device.CreateSourceVoice(format);
_wavBuffer = new(unchecked((int)wavStream.Length)); _wavBuffer = new(unchecked((int)wavStream.Length));
var bufSpan = _wavBuffer.AsSpan();
wavStream.Read(_wavBuffer.AsSpan()); var bytesRead = wavStream.Read(bufSpan);
Debug.Assert(bytesRead == bufSpan.Length, "reached end-of-file while reading .wav");
if (wavStream.Format == SDL2WavStream.AudioFormat.S16MSB) if (wavStream.Format == SDL2WavStream.AudioFormat.S16MSB)
{ {
EndiannessUtils.MutatingByteSwap16(_wavBuffer.AsSpan()); EndiannessUtils.MutatingByteSwap16(bufSpan);
} }
_wavVoice.SubmitSourceBuffer(_wavBuffer); _wavVoice.SubmitSourceBuffer(_wavBuffer);

View File

@ -1,3 +1,4 @@
using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -94,11 +95,13 @@ namespace BizHawk.Client.Common
private static unsafe T GetObject<T>(Stream s) private static unsafe T GetObject<T>(Stream s)
{ {
byte[] tmp = new byte[Marshal.SizeOf(typeof(T))]; var t = typeof(T);
s.Read(tmp, 0, tmp.Length); byte[] tmp = new byte[Marshal.SizeOf(t)];
var bytesRead = s.Read(tmp, offset: 0, count: tmp.Length);
Debug.Assert(bytesRead == tmp.Length, $"reached end-of-file while reading {t.Name}");
fixed (byte* p = tmp) fixed (byte* p = tmp)
{ {
return (T)Marshal.PtrToStructure((IntPtr)p, typeof(T)); return (T) Marshal.PtrToStructure((IntPtr) p, t);
} }
} }
@ -247,7 +250,8 @@ namespace BizHawk.Client.Common
int inH = bi.biHeight; int inH = bi.biHeight;
byte[] src = new byte[inW * inH * 4]; byte[] src = new byte[inW * inH * 4];
s.Read(src, 0, src.Length); var bytesRead = s.Read(src, offset: 0, count: src.Length);
Debug.Assert(bytesRead == src.Length, "reached end-of-file while reading .bmp");
if (v is LoadedBMP) if (v is LoadedBMP)
{ {
var l = v as LoadedBMP; var l = v as LoadedBMP;

View File

@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
@ -93,7 +94,8 @@ namespace BizHawk.Client.Common
// read the entire file into FileData. // read the entire file into FileData.
FileData = new byte[fileLength]; FileData = new byte[fileLength];
stream.Position = 0; stream.Position = 0;
stream.Read(FileData, 0, fileLength); var bytesRead = stream.Read(FileData, offset: 0, count: fileLength);
Debug.Assert(bytesRead == fileLength, "failed to read whole rom stream");
string SHA1_check = SHA1Checksum.ComputePrefixedHex(FileData); string SHA1_check = SHA1Checksum.ComputePrefixedHex(FileData);

View File

@ -40,7 +40,7 @@ namespace BizHawk.Client.Common
// looking for magic bytes // looking for magic bytes
fs.Seek(0x101, SeekOrigin.Begin); fs.Seek(0x101, SeekOrigin.Begin);
var buffer = new byte[8]; var buffer = new byte[8];
fs.Read(buffer, 0, 8); _ = fs.Read(buffer, offset: 0, count: buffer.Length); // if stream is too short, the next check will catch it
var s = buffer.BytesToHexString(); var s = buffer.BytesToHexString();
if (s == "7573746172003030" || s == "7573746172202000") return true; // "ustar\000" (libarchive's bsdtar) or "ustar \0" (GNU Tar) if (s == "7573746172003030" || s == "7573746172202000") return true; // "ustar\000" (libarchive's bsdtar) or "ustar \0" (GNU Tar)
@ -69,7 +69,7 @@ namespace BizHawk.Client.Common
var seekPos = fileStream.Position; var seekPos = fileStream.Position;
fileStream.Seek(0x101, SeekOrigin.Begin); fileStream.Seek(0x101, SeekOrigin.Begin);
var buffer = new byte[8]; var buffer = new byte[8];
fileStream.Read(buffer, 0, 8); _ = fileStream.Read(buffer, offset: 0, count: buffer.Length); // if stream is too short, the next check will catch it
fileStream.Seek(seekPos, SeekOrigin.Begin); fileStream.Seek(seekPos, SeekOrigin.Begin);
var s = buffer.BytesToHexString(); var s = buffer.BytesToHexString();
if (s == "7573746172003030" || s == "7573746172202000") return true; // "ustar\000" (libarchive's bsdtar) or "ustar \0" (GNU Tar) if (s == "7573746172003030" || s == "7573746172202000") return true; // "ustar\000" (libarchive's bsdtar) or "ustar \0" (GNU Tar)

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
@ -35,7 +36,8 @@ namespace BizHawk.Client.Common.movie.import
using var fs = new FileStream(SourceFile.FullName, FileMode.Open, FileAccess.Read); using var fs = new FileStream(SourceFile.FullName, FileMode.Open, FileAccess.Read);
{ {
byte[] data = new byte[4]; byte[] data = new byte[4];
fs.Read(data, 0, 4); var bytesRead = fs.Read(data, offset: 0, count: data.Length);
Debug.Assert(bytesRead == data.Length, "reached end-of-file while reading header");
if (!data.SequenceEqual(Zipheader)) if (!data.SequenceEqual(Zipheader))
{ {
Result.Errors.Add("This is not a zip file."); Result.Errors.Add("This is not a zip file.");

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using BizHawk.Common; using BizHawk.Common;
@ -15,7 +16,8 @@ namespace BizHawk.Client.Common
{ {
byte[] bytes = new byte[_streams[key].Length]; byte[] bytes = new byte[_streams[key].Length];
_streams[key].Seek(0, SeekOrigin.Begin); _streams[key].Seek(0, SeekOrigin.Begin);
_streams[key].Read(bytes, 0, bytes.Length); var bytesRead = _streams[key].Read(bytes, offset: 0, count: bytes.Length);
Debug.Assert(bytesRead == bytes.Length, "reached end-of-file while reading state");
return bytes; return bytes;
} }
set => SetState(key, new MemoryStream(value)); set => SetState(key, new MemoryStream(value));

View File

@ -78,7 +78,7 @@ namespace BizHawk.Client.Common
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{ {
byte[] data = new byte[4]; byte[] data = new byte[4];
fs.Read(data, 0, 4); _ = fs.Read(data, offset: 0, count: data.Length); // if stream is too short, the next check will catch it
if (!data.SequenceEqual(Zipheader)) if (!data.SequenceEqual(Zipheader))
{ {
return null; return null;

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -432,7 +433,8 @@ namespace BizHawk.Client.EmuHawk
Marshal.Copy(optional_program_id, programIdBytes, 0, 8); Marshal.Copy(optional_program_id, programIdBytes, 0, 8);
var programId = BitConverter.ToUInt64(programIdBytes, 0); var programId = BitConverter.ToUInt64(programIdBytes, 0);
using var seeddb = new BinaryReader(GetFirmware(new("3DS", "seeddb"))); FirmwareID seeddbFWID = new("3DS", "seeddb");
using BinaryReader seeddb = new(GetFirmware(seeddbFWID));
var count = seeddb.ReadUInt32(); var count = seeddb.ReadUInt32();
seeddb.BaseStream.Seek(12, SeekOrigin.Current); // apparently some padding bytes before actual seeds seeddb.BaseStream.Seek(12, SeekOrigin.Current); // apparently some padding bytes before actual seeds
for (long i = 0; i < count; i++) for (long i = 0; i < count; i++)
@ -446,7 +448,8 @@ namespace BizHawk.Client.EmuHawk
var sha256Input = new byte[32]; var sha256Input = new byte[32];
Marshal.Copy(primary_key_y, sha256Input, 0, 16); Marshal.Copy(primary_key_y, sha256Input, 0, 16);
seeddb.BaseStream.Read(sha256Input, 16, 16); var bytesRead = seeddb.BaseStream.Read(sha256Input, offset: 16, count: 16);
Debug.Assert(bytesRead is 16, $"reached end-of-file while reading {seeddbFWID} firmware");
var sha256Digest = SHA256Checksum.Compute(sha256Input); var sha256Digest = SHA256Checksum.Compute(sha256Input);
var secondaryKeyYBytes = new byte[17]; var secondaryKeyYBytes = new byte[17];

View File

@ -55,7 +55,8 @@ namespace BizHawk.Common
if (src.Read(tmp, 0, 2) != 2) throw new InvalidOperationException("Unexpected end of stream"); if (src.Read(tmp, 0, 2) != 2) throw new InvalidOperationException("Unexpected end of stream");
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);
src.Read(tmp, 0, 4); var bytesRead = src.Read(tmp, offset: 0, count: tmp.Length);
Debug.Assert(bytesRead == tmp.Length);
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

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

View File

@ -1,6 +1,7 @@
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using BizHawk.Common.PathExtensions; using BizHawk.Common.PathExtensions;
@ -114,7 +115,7 @@ namespace BizHawk.Emulation.DiscSystem
var bc = EndianBitConverter.CreateForLittleEndian(); var bc = EndianBitConverter.CreateForLittleEndian();
var header = new byte[88]; var header = new byte[88];
stream.Read(header, 0, 88); _ = stream.Read(header, offset: 0, count: header.Length); // stream size checked at callsite
this.Signature = Encoding.ASCII.GetString(header.Take(16).ToArray()); this.Signature = Encoding.ASCII.GetString(header.Take(16).ToArray());
this.Version = header.Skip(16).Take(2).ToArray(); this.Version = header.Skip(16).Take(2).ToArray();
@ -338,7 +339,8 @@ namespace BizHawk.Emulation.DiscSystem
for (var se = 0; se < aFile.Header.SessionCount; se++) for (var se = 0; se < aFile.Header.SessionCount; se++)
{ {
var sessionHeader = new byte[24]; var sessionHeader = new byte[24];
stream.Read(sessionHeader, 0, 24); var bytesRead = stream.Read(sessionHeader, offset: 0, count: sessionHeader.Length);
Debug.Assert(bytesRead == sessionHeader.Length, "reached end-of-file while reading session header");
//sessionHeader.Reverse().ToArray(); //sessionHeader.Reverse().ToArray();
var session = new ASession var session = new ASession
@ -374,7 +376,8 @@ namespace BizHawk.Emulation.DiscSystem
var trackHeader = new byte[80]; var trackHeader = new byte[80];
var track = new ATrack(); var track = new ATrack();
stream.Read(trackHeader, 0, 80); var bytesRead = stream.Read(trackHeader, offset: 0, count: trackHeader.Length);
Debug.Assert(bytesRead == trackHeader.Length, "reached end-of-file while reading track header");
track.Mode = trackHeader[0]; track.Mode = trackHeader[0];
track.SubMode = trackHeader[1]; track.SubMode = trackHeader[1];
@ -404,7 +407,8 @@ namespace BizHawk.Emulation.DiscSystem
{ {
var extHeader = new byte[8]; var extHeader = new byte[8];
stream.Seek(track.ExtraOffset, SeekOrigin.Begin); stream.Seek(track.ExtraOffset, SeekOrigin.Begin);
stream.Read(extHeader, 0, 8); var bytesRead1 = stream.Read(extHeader, offset: 0, count: extHeader.Length);
Debug.Assert(bytesRead1 == extHeader.Length, "reached end-of-file while reading extra block of track");
track.ExtraBlock.Pregap = bc.ToInt32(extHeader.Take(4).ToArray()); track.ExtraBlock.Pregap = bc.ToInt32(extHeader.Take(4).ToArray());
track.ExtraBlock.Sectors = bc.ToInt32(extHeader.Skip(4).Take(4).ToArray()); track.ExtraBlock.Sectors = bc.ToInt32(extHeader.Skip(4).Take(4).ToArray());
stream.Seek(currPos, SeekOrigin.Begin); stream.Seek(currPos, SeekOrigin.Begin);
@ -425,7 +429,8 @@ namespace BizHawk.Emulation.DiscSystem
var foot = new byte[16]; var foot = new byte[16];
stream.Seek(track.FooterOffset, SeekOrigin.Begin); stream.Seek(track.FooterOffset, SeekOrigin.Begin);
stream.Read(foot, 0, 16); var bytesRead1 = stream.Read(foot, offset: 0, count: foot.Length);
Debug.Assert(bytesRead1 == foot.Length, "reached end-of-file while reading track footer");
var f = new AFooter var f = new AFooter
{ {
@ -465,7 +470,8 @@ namespace BizHawk.Emulation.DiscSystem
// read the filename // read the filename
stream.Read(fname, 0, fname.Length); var bytesRead2 = stream.Read(fname, offset: 0, count: fname.Length);
Debug.Assert(bytesRead2 == fname.Length, "reached end-of-file while reading track filename");
// if widechar is 1 filename is stored using 16-bit, otherwise 8-bit is used // if widechar is 1 filename is stored using 16-bit, otherwise 8-bit is used
if (f.WideChar == 1) if (f.WideChar == 1)

View File

@ -511,7 +511,7 @@ namespace BizHawk.Tests.Client.Common.Movie
Assert.AreEqual(0, state.Frame); Assert.AreEqual(0, state.Frame);
Assert.AreEqual(4, state.Size); Assert.AreEqual(4, state.Size);
var bb = new byte[2]; var bb = new byte[2];
state.GetReadStream().Read(bb, 0, 2); _ = state.GetReadStream().Read(bb, offset: 0, count: bb.Length);
Assert.AreEqual(1, bb[0]); Assert.AreEqual(1, bb[0]);
Assert.AreEqual(2, bb[1]); Assert.AreEqual(2, bb[1]);
} }