Double check some bounds checks

still have some issues with gen4 mystery gifts and some groundtile flagging (to be resolved later)
This commit is contained in:
Kurt
2024-03-13 00:08:10 -05:00
parent 7ac5da37b3
commit 1241e6eff6
9 changed files with 15 additions and 15 deletions

View File

@@ -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;

View File

@@ -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));
}

View File

@@ -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;

View File

@@ -678,7 +678,7 @@ private int GetGiftOffsetPCD(int index)
}
private Span<byte> GetCardSpanPGT(int index) => Data.Slice(GetGiftOffsetPGT(index), PGT.Size);
private Span<byte> GetCardSpanPCD(int index) => Data.Slice(GetGiftOffsetPCD(index), PGT.Size);
private Span<byte> 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());

View File

@@ -24,14 +24,14 @@ public sealed class BoxLayout7(SAV7 sav, Memory<byte> raw) : SaveBlock<SAV7>(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;
}

View File

@@ -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);
}

View File

@@ -122,9 +122,9 @@ public void SetTrainers(ReadOnlySpan<byte> 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;

View File

@@ -56,7 +56,7 @@ private static List<string> 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<string> 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]}");

View File

@@ -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];
}