Improve range checks

This commit is contained in:
Kurt 2021-06-24 23:48:35 -07:00
parent 285f86d7b2
commit 595966b090

View file

@ -169,7 +169,7 @@ namespace PKHeX.Core
/// <returns>Flag is Set (true) or not Set (false)</returns> /// <returns>Flag is Set (true) or not Set (false)</returns>
public virtual bool GetEventFlag(int flagNumber) public virtual bool GetEventFlag(int flagNumber)
{ {
if (flagNumber >= EventFlagMax) if ((uint)flagNumber >= EventFlagMax)
throw new ArgumentException($"Event Flag to get ({flagNumber}) is greater than max ({EventFlagMax})."); throw new ArgumentException($"Event Flag to get ({flagNumber}) is greater than max ({EventFlagMax}).");
return GetFlag(EventFlag + (flagNumber >> 3), flagNumber & 7); return GetFlag(EventFlag + (flagNumber >> 3), flagNumber & 7);
} }
@ -182,7 +182,7 @@ namespace PKHeX.Core
/// <remarks>Flag is Set (true) or not Set (false)</remarks> /// <remarks>Flag is Set (true) or not Set (false)</remarks>
public virtual void SetEventFlag(int flagNumber, bool value) public virtual void SetEventFlag(int flagNumber, bool value)
{ {
if (flagNumber >= EventFlagMax) if ((uint)flagNumber >= EventFlagMax)
throw new ArgumentException($"Event Flag to set ({flagNumber}) is greater than max ({EventFlagMax})."); throw new ArgumentException($"Event Flag to set ({flagNumber}) is greater than max ({EventFlagMax}).");
SetFlag(EventFlag + (flagNumber >> 3), flagNumber & 7, value); SetFlag(EventFlag + (flagNumber >> 3), flagNumber & 7, value);
} }
@ -289,7 +289,7 @@ namespace PKHeX.Core
get get
{ {
var count = PartyCount; var count = PartyCount;
if (count > MaxPartyCount) if ((uint)count > MaxPartyCount)
count = MaxPartyCount; count = MaxPartyCount;
PKM[] data = new PKM[count]; PKM[] data = new PKM[count];
@ -616,7 +616,7 @@ namespace PKHeX.Core
public void GetBoxSlotFromIndex(int index, out int box, out int slot) public void GetBoxSlotFromIndex(int index, out int box, out int slot)
{ {
box = index / BoxSlotCount; box = index / BoxSlotCount;
if (box >= BoxCount) if ((uint)box >= BoxCount)
throw new ArgumentOutOfRangeException(nameof(index)); throw new ArgumentOutOfRangeException(nameof(index));
slot = index % BoxSlotCount; slot = index % BoxSlotCount;
} }
@ -646,7 +646,7 @@ namespace PKHeX.Core
{ {
if (box == insertBeforeBox) // no movement required if (box == insertBeforeBox) // no movement required
return true; return true;
if (box >= BoxCount || insertBeforeBox >= BoxCount) // invalid box positions if ((uint)box >= BoxCount || (uint)insertBeforeBox >= BoxCount) // invalid box positions
return false; return false;
MoveBox(box, insertBeforeBox, BoxBuffer); MoveBox(box, insertBeforeBox, BoxBuffer);
@ -699,7 +699,7 @@ namespace PKHeX.Core
{ {
if (box1 == box2) // no movement required if (box1 == box2) // no movement required
return true; return true;
if (box1 >= BoxCount || box2 >= BoxCount) // invalid box positions if ((uint)box1 >= BoxCount || (uint)box2 >= BoxCount) // invalid box positions
return false; return false;
if (!IsBoxAbleToMove(box1) || !IsBoxAbleToMove(box2)) if (!IsBoxAbleToMove(box1) || !IsBoxAbleToMove(box2))
@ -830,7 +830,7 @@ namespace PKHeX.Core
/// </summary> /// </summary>
/// <param name="action">Modification to perform on a <see cref="PKM"/></param> /// <param name="action">Modification to perform on a <see cref="PKM"/></param>
/// <param name="BoxStart">Starting box; if not provided, will iterate from the first box.</param> /// <param name="BoxStart">Starting box; if not provided, will iterate from the first box.</param>
/// <param name="BoxEnd">Ending box; if not provided, will iterate to the end.</param> /// <param name="BoxEnd">Ending box (inclusive); if not provided, will iterate to the end.</param>
/// <returns>Count of modified <see cref="PKM"/> slots.</returns> /// <returns>Count of modified <see cref="PKM"/> slots.</returns>
public int ModifyBoxes(Action<PKM> action, int BoxStart = 0, int BoxEnd = -1) public int ModifyBoxes(Action<PKM> action, int BoxStart = 0, int BoxEnd = -1)
{ {
@ -869,7 +869,7 @@ namespace PKHeX.Core
public virtual int GetBoxWallpaper(int box) public virtual int GetBoxWallpaper(int box)
{ {
int offset = GetBoxWallpaperOffset(box); int offset = GetBoxWallpaperOffset(box);
if (offset < 0 || box > BoxCount) if (offset < 0 || (uint)box > BoxCount)
return box; return box;
return Data[offset]; return Data[offset];
} }
@ -877,7 +877,7 @@ namespace PKHeX.Core
public virtual void SetBoxWallpaper(int box, int value) public virtual void SetBoxWallpaper(int box, int value)
{ {
int offset = GetBoxWallpaperOffset(box); int offset = GetBoxWallpaperOffset(box);
if (offset < 0 || box > BoxCount) if (offset < 0 || (uint)box > BoxCount)
return; return;
Data[offset] = (byte)value; Data[offset] = (byte)value;
} }