Fix some issues found by static code analysis

This commit is contained in:
Alex Barney 2022-11-22 10:38:46 -07:00
parent 2e769ea70a
commit eccd91bc15
14 changed files with 27 additions and 23 deletions

View file

@ -25,8 +25,8 @@ internal partial class ResultNameResolver : Result.IResultNameResolver
private static byte[] DecompressArchive() private static byte[] DecompressArchive()
{ {
var deflateStream = new DeflateStream(new MemoryStream(ArchiveData.ToArray()), CompressionMode.Decompress); using var deflateStream = new DeflateStream(new MemoryStream(ArchiveData.ToArray()), CompressionMode.Decompress);
var archiveDataStream = new MemoryStream(); using var archiveDataStream = new MemoryStream();
deflateStream.CopyTo(archiveDataStream); deflateStream.CopyTo(archiveDataStream);
return archiveDataStream.ToArray(); return archiveDataStream.ToArray();
} }

View file

@ -284,7 +284,7 @@ public partial class BucketTree
l2NodeHeader.EntryCount = indexInL2Node != 0 ? indexInL2Node : _offsetsPerNode; l2NodeHeader.EntryCount = indexInL2Node != 0 ? indexInL2Node : _offsetsPerNode;
l2NodeHeader.OffsetEnd = endOffset; l2NodeHeader.OffsetEnd = endOffset;
long l2NodeStorageOffset = _nodeSize * (l2NodeIndex + 1); long l2NodeStorageOffset = (long)_nodeSize * (l2NodeIndex + 1);
res = _nodeStorage.Write(l2NodeStorageOffset, _l2Node.GetBuffer()); res = _nodeStorage.Write(l2NodeStorageOffset, _l2Node.GetBuffer());
if (res.IsFailure()) return res.Miss(); if (res.IsFailure()) return res.Miss();
} }

View file

@ -291,6 +291,8 @@ public class CompressedStorage : IStorage, IAsynchronousAccessSplitter
FinalizeObject(); FinalizeObject();
_cacheManager.Dispose(); _cacheManager.Dispose();
_core.Dispose(); _core.Dispose();
base.Dispose();
} }
public Result Initialize(MemoryResource allocatorForBucketTree, IBufferManager allocatorForCacheManager, public Result Initialize(MemoryResource allocatorForBucketTree, IBufferManager allocatorForCacheManager,

View file

@ -92,7 +92,7 @@ public struct NcaHeader
public Array4<Hash> FsHeaderHashes; public Array4<Hash> FsHeaderHashes;
public Array256<byte> EncryptedKeys; public Array256<byte> EncryptedKeys;
public static ulong SectorToByte(uint sectorIndex) => sectorIndex << SectorShift; public static ulong SectorToByte(uint sectorIndex) => (ulong)sectorIndex << SectorShift;
public static uint ByteToSector(ulong byteIndex) => (uint)(byteIndex >> SectorShift); public static uint ByteToSector(ulong byteIndex) => (uint)(byteIndex >> SectorShift);
public readonly byte GetProperKeyGeneration() => Math.Max(KeyGeneration1, KeyGeneration2); public readonly byte GetProperKeyGeneration() => Math.Max(KeyGeneration1, KeyGeneration2);

View file

@ -204,7 +204,7 @@ public class NcaReader : IDisposable
Assert.SdkRequiresInRange(index, 0, NcaHeader.FsCountMax); Assert.SdkRequiresInRange(index, 0, NcaHeader.FsCountMax);
long offset = Unsafe.SizeOf<NcaHeader>() + Unsafe.SizeOf<NcaFsHeader>() * index; long offset = Unsafe.SizeOf<NcaHeader>() + Unsafe.SizeOf<NcaFsHeader>() * (long)index;
return _headerStorage.Get.Read(offset, SpanHelpers.AsByteSpan(ref outHeader)); return _headerStorage.Get.Read(offset, SpanHelpers.AsByteSpan(ref outHeader));
} }

View file

@ -93,10 +93,7 @@ public class AesXtsFileHeader
private void GenerateKek(byte[] kekSeed, string path) private void GenerateKek(byte[] kekSeed, string path)
{ {
var hash = new HMACSHA256(kekSeed); byte[] checksum = HMACSHA256.HashData(kekSeed, Encoding.UTF8.GetBytes(path));
byte[] pathBytes = Encoding.UTF8.GetBytes(path);
byte[] checksum = hash.ComputeHash(pathBytes, 0, pathBytes.Length);
Array.Copy(checksum, 0, Kek1, 0, 0x10); Array.Copy(checksum, 0, Kek1, 0, 0x10);
Array.Copy(checksum, 0x10, Kek2, 0, 0x10); Array.Copy(checksum, 0x10, Kek2, 0, 0x10);
} }
@ -104,9 +101,8 @@ public class AesXtsFileHeader
private byte[] CalculateHmac(byte[] key) private byte[] CalculateHmac(byte[] key)
{ {
byte[] message = ToBytes(true).AsSpan(0x20).ToArray(); byte[] message = ToBytes(true).AsSpan(0x20).ToArray();
var hash = new HMACSHA256(message);
return hash.ComputeHash(key); return HMACSHA256.HashData(message, key);
} }
public byte[] ToBytes(bool writeDecryptedKey) public byte[] ToBytes(bool writeDecryptedKey)

View file

@ -90,7 +90,7 @@ public class HierarchicalIntegrityVerificationStorage : IStorage
{ {
Data = levels[i], Data = levels[i],
BlockSize = 1 << ivfc.LevelHeaders[i - 1].BlockSizePower, BlockSize = 1 << ivfc.LevelHeaders[i - 1].BlockSizePower,
Salt = new HMACSHA256(Encoding.ASCII.GetBytes(SaltSources[i - 1])).ComputeHash(ivfc.SaltSource), Salt = HMACSHA256.HashData(Encoding.ASCII.GetBytes(SaltSources[i - 1]), ivfc.SaltSource),
Type = type Type = type
}; };
} }

View file

@ -82,7 +82,7 @@ public static class NcaExtensions
expectedHash = ivfcInfo.MasterHash.ToArray(); expectedHash = ivfcInfo.MasterHash.ToArray();
offset = ivfcInfo.GetLevelOffset(0); offset = ivfcInfo.GetLevelOffset(0);
size = 1 << ivfcInfo.GetLevelBlockSize(0); size = 1L << ivfcInfo.GetLevelBlockSize(0);
break; break;
case NcaHashType.Sha256: case NcaHashType.Sha256:

View file

@ -21,7 +21,7 @@ public class AllocationTableStorage : IStorage
Fat = table; Fat = table;
InitialBlock = initialBlock; InitialBlock = initialBlock;
_length = initialBlock == -1 ? 0 : table.GetListLength(initialBlock) * blockSize; _length = initialBlock == -1 ? 0 : (long)table.GetListLength(initialBlock) * blockSize;
} }
public override Result Read(long offset, Span<byte> destination) public override Result Read(long offset, Span<byte> destination)
@ -42,7 +42,7 @@ public class AllocationTableStorage : IStorage
} }
int segmentPos = (int)(inPos - (long)iterator.VirtualBlock * BlockSize); int segmentPos = (int)(inPos - (long)iterator.VirtualBlock * BlockSize);
long physicalOffset = iterator.PhysicalBlock * BlockSize + segmentPos; long physicalOffset = (long)iterator.PhysicalBlock * BlockSize + segmentPos;
int remainingInSegment = iterator.CurrentSegmentSize * BlockSize - segmentPos; int remainingInSegment = iterator.CurrentSegmentSize * BlockSize - segmentPos;
int bytesToRead = Math.Min(remaining, remainingInSegment); int bytesToRead = Math.Min(remaining, remainingInSegment);
@ -76,7 +76,7 @@ public class AllocationTableStorage : IStorage
} }
int segmentPos = (int)(inPos - (long)iterator.VirtualBlock * BlockSize); int segmentPos = (int)(inPos - (long)iterator.VirtualBlock * BlockSize);
long physicalOffset = iterator.PhysicalBlock * BlockSize + segmentPos; long physicalOffset = (long)iterator.PhysicalBlock * BlockSize + segmentPos;
int remainingInSegment = iterator.CurrentSegmentSize * BlockSize - segmentPos; int remainingInSegment = iterator.CurrentSegmentSize * BlockSize - segmentPos;
int bytesToWrite = Math.Min(remaining, remainingInSegment); int bytesToWrite = Math.Min(remaining, remainingInSegment);
@ -115,7 +115,7 @@ public class AllocationTableStorage : IStorage
InitialBlock = Fat.Allocate(newBlockCount); InitialBlock = Fat.Allocate(newBlockCount);
if (InitialBlock == -1) throw new IOException("Not enough space to resize file."); if (InitialBlock == -1) throw new IOException("Not enough space to resize file.");
_length = newBlockCount * BlockSize; _length = (long)newBlockCount * BlockSize;
return Result.Success; return Result.Success;
} }
@ -143,7 +143,7 @@ public class AllocationTableStorage : IStorage
Fat.Free(oldBlocks); Fat.Free(oldBlocks);
} }
_length = newBlockCount * BlockSize; _length = (long)newBlockCount * BlockSize;
return Result.Success; return Result.Success;
} }

View file

@ -48,7 +48,7 @@ public class JournalStorage : IStorage
int blockNum = (int)(inPos / BlockSize); int blockNum = (int)(inPos / BlockSize);
int blockPos = (int)(inPos % BlockSize); int blockPos = (int)(inPos % BlockSize);
long physicalOffset = Map.GetPhysicalBlock(blockNum) * BlockSize + blockPos; long physicalOffset = (long)Map.GetPhysicalBlock(blockNum) * BlockSize + blockPos;
int bytesToRead = Math.Min(remaining, BlockSize - blockPos); int bytesToRead = Math.Min(remaining, BlockSize - blockPos);
@ -77,7 +77,7 @@ public class JournalStorage : IStorage
int blockNum = (int)(inPos / BlockSize); int blockNum = (int)(inPos / BlockSize);
int blockPos = (int)(inPos % BlockSize); int blockPos = (int)(inPos % BlockSize);
long physicalOffset = Map.GetPhysicalBlock(blockNum) * BlockSize + blockPos; long physicalOffset = (long)Map.GetPhysicalBlock(blockNum) * BlockSize + blockPos;
int bytesToWrite = Math.Min(remaining, BlockSize - blockPos); int bytesToWrite = Math.Min(remaining, BlockSize - blockPos);

View file

@ -140,6 +140,8 @@ public class RemapStorage : IStorage
{ {
BaseStorage?.Dispose(); BaseStorage?.Dispose();
} }
base.Dispose();
} }
public IStorage GetBaseStorage() => BaseStorage; public IStorage GetBaseStorage() => BaseStorage;
@ -211,7 +213,7 @@ public class RemapStorage : IStorage
private long GetOffsetMask() private long GetOffsetMask()
{ {
return (1 << (64 - Header.SegmentBits)) - 1; return (1L << (64 - Header.SegmentBits)) - 1;
} }
private long GetSegmentMask() private long GetSegmentMask()

View file

@ -76,6 +76,8 @@ public class SectorStorage : IStorage
{ {
BaseStorage?.Dispose(); BaseStorage?.Dispose();
} }
base.Dispose();
} }
/// <summary> /// <summary>

View file

@ -83,5 +83,7 @@ public class StreamStorage : IStorage
{ {
BaseStream?.Dispose(); BaseStream?.Dispose();
} }
base.Dispose();
} }
} }

View file

@ -94,7 +94,7 @@ public class KernelAccessControl
descriptor >>= 7; descriptor >>= 7;
ulong mmioSize = (descriptor & 0xffffff) << 12; ulong mmioSize = (descriptor & 0xffffffUL) << 12;
bool isNormal = (descriptor >> 24) != 0; bool isNormal = (descriptor >> 24) != 0;
items[index].NormalMmio.Add(new KernelAccessControlMmio(address, mmioSize, isRo, isNormal)); items[index].NormalMmio.Add(new KernelAccessControlMmio(address, mmioSize, isRo, isNormal));
@ -107,7 +107,7 @@ public class KernelAccessControl
//Map Normal Page. //Map Normal Page.
case 7: case 7:
{ {
ulong address = descriptor << 12; ulong address = (ulong)descriptor << 12;
items[index].PageMmio.Add(new KernelAccessControlMmio(address, 0x1000, false, false)); items[index].PageMmio.Add(new KernelAccessControlMmio(address, 0x1000, false, false));