From 1241e6eff63f255b44dbf23f020dd1dda872235e Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 13 Mar 2024 00:08:10 -0500 Subject: [PATCH] Double check some bounds checks still have some issues with gen4 mystery gifts and some groundtile flagging (to be resolved later) --- PKHeX.Core/Game/GameStrings/GameLanguage.cs | 2 +- PKHeX.Core/Saves/SAV1Stadium.cs | 4 ++-- PKHeX.Core/Saves/SAV3.cs | 4 ++-- PKHeX.Core/Saves/SAV4.cs | 2 +- PKHeX.Core/Saves/Substructures/Gen7/BoxLayout7.cs | 4 ++-- PKHeX.Core/Saves/Substructures/Gen8/BS/BoxLayout8b.cs | 4 ++-- PKHeX.Core/Saves/Substructures/Gen8/BS/UgSaveData8b.cs | 4 ++-- PKHeX.Core/Saves/Util/SaveExtensions.cs | 4 ++-- PKHeX.WinForms/Controls/Slots/PokePreview.cs | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/PKHeX.Core/Game/GameStrings/GameLanguage.cs b/PKHeX.Core/Game/GameStrings/GameLanguage.cs index b169f6347..77deb620f 100644 --- a/PKHeX.Core/Game/GameStrings/GameLanguage.cs +++ b/PKHeX.Core/Game/GameStrings/GameLanguage.cs @@ -9,7 +9,7 @@ public static class GameLanguage { public const string DefaultLanguage = "en"; // English public static int DefaultLanguageIndex => Array.IndexOf(LanguageCodes, DefaultLanguage); - public static string Language2Char(int lang) => lang > LanguageCodes.Length ? DefaultLanguage : LanguageCodes[lang]; + public static string Language2Char(int lang) => (uint)lang >= LanguageCodes.Length ? DefaultLanguage : LanguageCodes[lang]; public static int LanguageCount => LanguageCodes.Length; diff --git a/PKHeX.Core/Saves/SAV1Stadium.cs b/PKHeX.Core/Saves/SAV1Stadium.cs index da4f02ccf..22d70babe 100644 --- a/PKHeX.Core/Saves/SAV1Stadium.cs +++ b/PKHeX.Core/Saves/SAV1Stadium.cs @@ -140,14 +140,14 @@ public override byte[] GetDataForFormatStored(PKM pk) private int GetTeamOffsetJ(int team) { - if ((uint) team > TeamCount) + if ((uint) team >= TeamCount) throw new ArgumentOutOfRangeException(nameof(team)); return GetTeamTypeOffsetJ(team / TeamCountJ) + (TeamSizeJ * (team % TeamCountJ)); } private int GetTeamOffsetU(int team) { - if ((uint)team > TeamCount) + if ((uint)team >= TeamCount) throw new ArgumentOutOfRangeException(nameof(team)); return GetTeamTypeOffsetU(team / TeamCountU) + (TeamSizeU * (team % TeamCountU)); } diff --git a/PKHeX.Core/Saves/SAV3.cs b/PKHeX.Core/Saves/SAV3.cs index ee401866b..184fb2c2d 100644 --- a/PKHeX.Core/Saves/SAV3.cs +++ b/PKHeX.Core/Saves/SAV3.cs @@ -462,7 +462,7 @@ public sealed override int CurrentBox public int GetBoxWallpaper(int box) { - if (box > COUNT_BOX) + if (box >= COUNT_BOX) return box; int offset = GetBoxWallpaperOffset(box); return Storage[offset]; @@ -472,7 +472,7 @@ public int GetBoxWallpaper(int box) public void SetBoxWallpaper(int box, int value) { - if (box > COUNT_BOX) + if (box >= COUNT_BOX) return; int offset = GetBoxWallpaperOffset(box); Storage[offset] = (byte)value; diff --git a/PKHeX.Core/Saves/SAV4.cs b/PKHeX.Core/Saves/SAV4.cs index 01a582603..36dd5bde3 100644 --- a/PKHeX.Core/Saves/SAV4.cs +++ b/PKHeX.Core/Saves/SAV4.cs @@ -678,7 +678,7 @@ private int GetGiftOffsetPCD(int index) } private Span GetCardSpanPGT(int index) => Data.Slice(GetGiftOffsetPGT(index), PGT.Size); - private Span GetCardSpanPCD(int index) => Data.Slice(GetGiftOffsetPCD(index), PGT.Size); + private Span GetCardSpanPCD(int index) => Data.Slice(GetGiftOffsetPCD(index), PCD.Size); public PGT GetMysteryGiftPGT(int index) => new(GetCardSpanPGT(index).ToArray()); public PCD GetMysteryGiftPCD(int index) => new(GetCardSpanPCD(index).ToArray()); diff --git a/PKHeX.Core/Saves/Substructures/Gen7/BoxLayout7.cs b/PKHeX.Core/Saves/Substructures/Gen7/BoxLayout7.cs index ab041144a..4faf726c9 100644 --- a/PKHeX.Core/Saves/Substructures/Gen7/BoxLayout7.cs +++ b/PKHeX.Core/Saves/Substructures/Gen7/BoxLayout7.cs @@ -24,14 +24,14 @@ public sealed class BoxLayout7(SAV7 sav, Memory raw) : SaveBlock(sav public int GetBoxWallpaper(int box) { - if ((uint)box > SAV.BoxCount) + if ((uint)box >= SAV.BoxCount) return 0; return Data[GetBoxWallpaperOffset(box)]; } public void SetBoxWallpaper(int box, int value) { - if ((uint)box > SAV.BoxCount) + if ((uint)box >= SAV.BoxCount) return; Data[GetBoxWallpaperOffset(box)] = (byte)value; } diff --git a/PKHeX.Core/Saves/Substructures/Gen8/BS/BoxLayout8b.cs b/PKHeX.Core/Saves/Substructures/Gen8/BS/BoxLayout8b.cs index 44a211cad..c588fbb2f 100644 --- a/PKHeX.Core/Saves/Substructures/Gen8/BS/BoxLayout8b.cs +++ b/PKHeX.Core/Saves/Substructures/Gen8/BS/BoxLayout8b.cs @@ -137,14 +137,14 @@ public byte CurrentBox public int GetBoxWallpaper(int box) { - if ((uint)box > BoxCount) + if ((uint)box >= BoxCount) return 0; return Data[GetBoxWallpaperOffset(box)] - 1; } public void SetBoxWallpaper(int box, int value) { - if ((uint)box > BoxCount) + if ((uint)box >= BoxCount) return; Data[GetBoxWallpaperOffset(box)] = (byte)(value + 1); } diff --git a/PKHeX.Core/Saves/Substructures/Gen8/BS/UgSaveData8b.cs b/PKHeX.Core/Saves/Substructures/Gen8/BS/UgSaveData8b.cs index 519d00260..cf5f2d89f 100644 --- a/PKHeX.Core/Saves/Substructures/Gen8/BS/UgSaveData8b.cs +++ b/PKHeX.Core/Saves/Substructures/Gen8/BS/UgSaveData8b.cs @@ -122,9 +122,9 @@ public void SetTrainers(ReadOnlySpan data) public void FillNPC(byte value, int start = 0, int count = COUNT_TRAINERS) { - if ((uint)start + (uint)count > COUNT_TRAINERS) + if ((uint)start + (uint)count >= COUNT_TRAINERS) throw new ArgumentOutOfRangeException(nameof(count)); - if ((uint)start > COUNT_TRAINERS) + if ((uint)start >= COUNT_TRAINERS) throw new ArgumentOutOfRangeException(nameof(start)); var ofs = OFS_NPC + start; diff --git a/PKHeX.Core/Saves/Util/SaveExtensions.cs b/PKHeX.Core/Saves/Util/SaveExtensions.cs index 944058f69..2bda282ab 100644 --- a/PKHeX.Core/Saves/Util/SaveExtensions.cs +++ b/PKHeX.Core/Saves/Util/SaveExtensions.cs @@ -56,7 +56,7 @@ private static List GetSaveFileErrata(this SaveFile sav, PKM pk, IBasicS } } - if (pk.Species > strings.Species.Count) + if (pk.Species >= strings.Species.Count) errata.Add($"{MsgIndexSpeciesRange} {pk.Species}"); else if (sav.MaxSpeciesID < pk.Species) errata.Add($"{MsgIndexSpeciesGame} {strings.Species[pk.Species]}"); @@ -68,7 +68,7 @@ private static List GetSaveFileErrata(this SaveFile sav, PKM pk, IBasicS for (int i = 0; i < 4; i++) { var move = pk.GetMove(i); - if ((uint)move > movestr.Count) + if ((uint)move >= movestr.Count) errata.Add($"{MsgIndexMoveRange} {move}"); else if (move > sav.MaxMoveID) errata.Add($"{MsgIndexMoveGame} {movestr[move]}"); diff --git a/PKHeX.WinForms/Controls/Slots/PokePreview.cs b/PKHeX.WinForms/Controls/Slots/PokePreview.cs index 1814d3387..4a59c3118 100644 --- a/PKHeX.WinForms/Controls/Slots/PokePreview.cs +++ b/PKHeX.WinForms/Controls/Slots/PokePreview.cs @@ -82,7 +82,7 @@ private void PopulateGender(PKM pk) } var gender = pk.Gender; - if (gender > GenderImages.Length) + if (gender >= GenderImages.Length) gender = 2; PB_Gender.Image = GenderImages[gender]; }