From 49d42c5d1f8c598eb9b975f2048e2a73aff49412 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Tue, 9 Jun 2020 22:48:07 -0700 Subject: [PATCH] Fix some checks being ignored in savedata classes - Return an error when an allocation table seek fails - Fix a copy/paste bug to properly error when resizing an AllocationTableStorage with not enough free space --- build/CodeGen/results.csv | 1 + src/LibHac/Fs/ResultFs.cs | 2 ++ src/LibHac/FsSystem/Save/AllocationTableStorage.cs | 14 +++++++++++--- src/LibHac/FsSystem/Save/SaveDataFile.cs | 3 ++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/build/CodeGen/results.csv b/build/CodeGen/results.csv index 85ef30d2..57b6c099 100644 --- a/build/CodeGen/results.csv +++ b/build/CodeGen/results.csv @@ -113,6 +113,7 @@ Module,DescriptionStart,DescriptionEnd,Name,Summary 2,4722,,SaveDataAllocationTableCorruptedInternal, 2,4723,,SaveDataFileTableCorruptedInternal, 2,4724,,AllocationTableIteratedRangeEntryInternal, +2,4725,,InvalidAllocationTableOffset, 2,4741,4759,AesXtsFileSystemCorrupted, 2,4742,,AesXtsFileHeaderTooShort, diff --git a/src/LibHac/Fs/ResultFs.cs b/src/LibHac/Fs/ResultFs.cs index 00d083b4..419eceb3 100644 --- a/src/LibHac/Fs/ResultFs.cs +++ b/src/LibHac/Fs/ResultFs.cs @@ -229,6 +229,8 @@ namespace LibHac.Fs public static Result.Base SaveDataFileTableCorruptedInternal => new Result.Base(ModuleFs, 4723); /// Error code: 2002-4724; Inner value: 0x24e802 public static Result.Base AllocationTableIteratedRangeEntryInternal => new Result.Base(ModuleFs, 4724); + /// Error code: 2002-4725; Inner value: 0x24ea02 + public static Result.Base InvalidAllocationTableOffset => new Result.Base(ModuleFs, 4725); /// Error code: 2002-4741; Range: 4741-4759; Inner value: 0x250a02 public static Result.Base AesXtsFileSystemCorrupted { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new Result.Base(ModuleFs, 4741, 4759); } diff --git a/src/LibHac/FsSystem/Save/AllocationTableStorage.cs b/src/LibHac/FsSystem/Save/AllocationTableStorage.cs index f68dfe16..05066de1 100644 --- a/src/LibHac/FsSystem/Save/AllocationTableStorage.cs +++ b/src/LibHac/FsSystem/Save/AllocationTableStorage.cs @@ -34,7 +34,11 @@ namespace LibHac.FsSystem.Save while (remaining > 0) { int blockNum = (int)(inPos / BlockSize); - iterator.Seek(blockNum); + + if (!iterator.Seek(blockNum)) + { + return ResultFs.InvalidAllocationTableOffset.Log(); + } int segmentPos = (int)(inPos - (long)iterator.VirtualBlock * BlockSize); long physicalOffset = iterator.PhysicalBlock * BlockSize + segmentPos; @@ -64,7 +68,11 @@ namespace LibHac.FsSystem.Save while (remaining > 0) { int blockNum = (int)(inPos / BlockSize); - iterator.Seek(blockNum); + + if (!iterator.Seek(blockNum)) + { + return ResultFs.InvalidAllocationTableOffset.Log(); + } int segmentPos = (int)(inPos - (long)iterator.VirtualBlock * BlockSize); long physicalOffset = iterator.PhysicalBlock * BlockSize + segmentPos; @@ -124,7 +132,7 @@ namespace LibHac.FsSystem.Save if (newBlockCount > oldBlockCount) { int newBlocks = Fat.Allocate(newBlockCount - oldBlockCount); - if (InitialBlock == -1) throw new IOException("Not enough space to resize file."); + if (newBlocks == -1) throw new IOException("Not enough space to resize file."); Fat.Join(InitialBlock, newBlocks); } diff --git a/src/LibHac/FsSystem/Save/SaveDataFile.cs b/src/LibHac/FsSystem/Save/SaveDataFile.cs index 1718411f..6f40263e 100644 --- a/src/LibHac/FsSystem/Save/SaveDataFile.cs +++ b/src/LibHac/FsSystem/Save/SaveDataFile.cs @@ -53,7 +53,8 @@ namespace LibHac.FsSystem.Save if (rc.IsFailure()) return rc; } - BaseStorage.Write(offset, source); + rc = BaseStorage.Write(offset, source); + if (rc.IsFailure()) return rc; if ((options & WriteOption.Flush) != 0) {