Add some "unchecked" blocks

Try to identify places where integer overflow is expected and mark them as "unchecked"
This commit is contained in:
Alex Barney 2022-01-15 12:39:06 -07:00
parent 085511660d
commit d116903892
9 changed files with 53 additions and 39 deletions

View file

@ -566,7 +566,7 @@ public static class ExternalKeyReader
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsValidNameChar(char c) private static bool IsValidNameChar(char c)
{ {
return (c | 0x20u) - 'a' <= 'z' - 'a' || (uint)(c - '0') <= 9 || c == '_'; return unchecked((c | 0x20u) - 'a' <= 'z' - 'a' || (uint)(c - '0') <= 9 || c == '_');
} }
private static bool TryGetKeyInfo(out SpecificKeyInfo info, List<KeyInfo> keyList, ReadOnlySpan<char> keyName) private static bool TryGetKeyInfo(out SpecificKeyInfo info, List<KeyInfo> keyList, ReadOnlySpan<char> keyName)

View file

@ -147,7 +147,7 @@ public ref struct U8StringBuilder
// Remove possible sign extension if needed // Remove possible sign extension if needed
if (mask == 'x' || mask == 'X') if (mask == 'x' || mask == 'X')
{ {
value &= (long)mask; value &= unchecked((long)mask);
} }
int bytesWritten; int bytesWritten;

View file

@ -31,8 +31,8 @@ public struct AesCtrModeNi
ref Vector128<byte> inBlock = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(input)); ref Vector128<byte> inBlock = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(input));
ref Vector128<byte> outBlock = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(output)); ref Vector128<byte> outBlock = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(output));
Vector128<byte> byteSwapMask = Vector128.Create((ulong)0x706050403020100, 0x8090A0B0C0D0E0F).AsByte(); Vector128<byte> byteSwapMask = Vector128.Create(0x706050403020100ul, 0x8090A0B0C0D0E0Ful).AsByte();
var inc = Vector128.Create((ulong)0, 1); var inc = Vector128.Create(0ul, 1ul);
Vector128<byte> iv = Iv; Vector128<byte> iv = Iv;
Vector128<ulong> bSwappedIv = Ssse3.Shuffle(iv, byteSwapMask).AsUInt64(); Vector128<ulong> bSwappedIv = Ssse3.Shuffle(iv, byteSwapMask).AsUInt64();

View file

@ -154,6 +154,8 @@ public struct AesXtsMode
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Gf128Mul(ref Buffer16 buffer) private static void Gf128Mul(ref Buffer16 buffer)
{
unchecked
{ {
Span<ulong> b = buffer.AsSpan<ulong>(); Span<ulong> b = buffer.AsSpan<ulong>();
@ -162,6 +164,7 @@ public struct AesXtsMode
b[1] = (b[1] << 1) | (b[0] >> 63); b[1] = (b[1] << 1) | (b[0] >> 63);
b[0] = (b[0] << 1) ^ tt; b[0] = (b[0] << 1) ^ tt;
} }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void XorBuffer(ref Buffer16 output, ref Buffer16 input1, ref Buffer16 input2) private static void XorBuffer(ref Buffer16 output, ref Buffer16 input1, ref Buffer16 input2)

View file

@ -2166,7 +2166,7 @@ internal class SaveDataFileSystemService : ISaveDataTransferCoreInterface, ISave
private bool IsStaticSaveDataIdValueRange(ulong id) private bool IsStaticSaveDataIdValueRange(ulong id)
{ {
return (long)id < 0; return unchecked((long)id) < 0;
} }
private void ModifySaveDataExtraData(ref SaveDataExtraData currentExtraData, in SaveDataExtraData extraData, private void ModifySaveDataExtraData(ref SaveDataExtraData currentExtraData, in SaveDataExtraData extraData,

View file

@ -289,7 +289,9 @@ public readonly struct Result : IEquatable<Result>
/// The <see cref="Result"/> representing the start of this result range. /// The <see cref="Result"/> representing the start of this result range.
/// If returning a <see cref="Result"/> from a function, use <see cref="Log"/> instead. /// If returning a <see cref="Result"/> from a function, use <see cref="Log"/> instead.
/// </summary> /// </summary>
public Result Value => new Result((BaseType)_value); // The conversion to int won't remove the part of the value containing the description end, but that doesn't
// matter because the constructor for Result will take care of that.
public Result Value => new Result(unchecked((BaseType)_value));
/// <summary> /// <summary>
/// Checks if the range of this <see cref="Result.Base"/> includes the provided <see cref="Result"/>. /// Checks if the range of this <see cref="Result.Base"/> includes the provided <see cref="Result"/>.
@ -307,7 +309,7 @@ public readonly struct Result : IEquatable<Result>
} }
return result.Module == Module && return result.Module == Module &&
result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart; unchecked(result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart);
} }
/// <summary> /// <summary>
@ -356,7 +358,7 @@ public readonly struct Result : IEquatable<Result>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static BaseType GetBitsValueLong(ulong value, int bitsOffset, int bitsCount) private static BaseType GetBitsValueLong(ulong value, int bitsOffset, int bitsCount)
{ {
return (BaseType)(value >> bitsOffset) & ~(~default(BaseType) << bitsCount); return unchecked((BaseType)(value >> bitsOffset)) & ~(~default(BaseType) << bitsCount);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -391,7 +393,7 @@ public readonly struct Result : IEquatable<Result>
public BaseType DescriptionRangeStart => GetBitsValueLong(_value, DescriptionBitsOffset, DescriptionBitsCount); public BaseType DescriptionRangeStart => GetBitsValueLong(_value, DescriptionBitsOffset, DescriptionBitsCount);
public BaseType DescriptionRangeEnd => GetBitsValueLong(_value, DescriptionEndBitsOffset, DescriptionBitsCount); public BaseType DescriptionRangeEnd => GetBitsValueLong(_value, DescriptionEndBitsOffset, DescriptionBitsCount);
private Result Value => new Result((BaseType)_value); private Result Value => new Result(unchecked((BaseType)_value));
/// <summary> /// <summary>
/// Checks if the range of this <see cref="Result.Base"/> includes the provided <see cref="Result"/>. /// Checks if the range of this <see cref="Result.Base"/> includes the provided <see cref="Result"/>.
@ -409,7 +411,7 @@ public readonly struct Result : IEquatable<Result>
} }
return result.Module == Module && return result.Module == Module &&
result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart; unchecked(result.Description - DescriptionRangeStart <= DescriptionRangeEnd - DescriptionRangeStart);
} }
} }
} }

View file

@ -174,7 +174,7 @@ public class Aes128XtsTransform
for (int i = 2; i < bufL.Length; i += 2) for (int i = 2; i < bufL.Length; i += 2)
{ {
ulong tt = (ulong)((long)a >> 63) & 0x87; ulong tt = unchecked((ulong)((long)a >> 63) & 0x87);
a = (a << 1) | (b >> 63); a = (a << 1) | (b >> 63);
b = (b << 1) ^ tt; b = (b << 1) ^ tt;
@ -193,7 +193,7 @@ public class Aes128XtsTransform
{ {
for (int i = 0xF; i >= 0; i--) for (int i = 0xF; i >= 0; i--)
{ {
value[i] = (byte)sector; value[i] = unchecked((byte)sector);
sector >>= 8; sector >>= 8;
} }
} }

View file

@ -63,6 +63,8 @@ internal static class HexConverter
// furthest access). // furthest access).
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToBytesBuffer(byte value, Span<byte> buffer, int startingIndex = 0, Casing casing = Casing.Upper) public static void ToBytesBuffer(byte value, Span<byte> buffer, int startingIndex = 0, Casing casing = Casing.Upper)
{
unchecked
{ {
uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U; uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U;
uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing; uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
@ -70,9 +72,12 @@ internal static class HexConverter
buffer[startingIndex + 1] = (byte)packedResult; buffer[startingIndex + 1] = (byte)packedResult;
buffer[startingIndex] = (byte)(packedResult >> 8); buffer[startingIndex] = (byte)(packedResult >> 8);
} }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ToCharsBuffer(byte value, Span<char> buffer, int startingIndex = 0, Casing casing = Casing.Upper) public static void ToCharsBuffer(byte value, Span<char> buffer, int startingIndex = 0, Casing casing = Casing.Upper)
{
unchecked
{ {
uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U; uint difference = ((value & 0xF0U) << 4) + (value & 0x0FU) - 0x8989U;
uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing; uint packedResult = ((((uint)(-(int)difference) & 0x7070U) >> 4) + difference + 0xB9B9U) | (uint)casing;
@ -80,6 +85,7 @@ internal static class HexConverter
buffer[startingIndex + 1] = (char)(packedResult & 0xFF); buffer[startingIndex + 1] = (char)(packedResult & 0xFF);
buffer[startingIndex] = (char)(packedResult >> 8); buffer[startingIndex] = (char)(packedResult >> 8);
} }
}
public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing = Casing.Upper) public static void EncodeToUtf16(ReadOnlySpan<byte> bytes, Span<char> chars, Casing casing = Casing.Upper)
{ {
@ -181,6 +187,8 @@ internal static class HexConverter
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int FromLowerChar(int c) public static int FromLowerChar(int c)
{
unchecked
{ {
if ((uint)(c - '0') <= '9' - '0') if ((uint)(c - '0') <= '9' - '0')
return c - '0'; return c - '0';
@ -190,6 +198,7 @@ internal static class HexConverter
return 0xFF; return 0xFF;
} }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsHexChar(int c) public static bool IsHexChar(int c)
@ -200,13 +209,13 @@ internal static class HexConverter
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsHexUpperChar(int c) public static bool IsHexUpperChar(int c)
{ {
return (uint)(c - '0') <= 9 || (uint)(c - 'A') <= ('F' - 'A'); return unchecked((uint)(c - '0') <= 9 || (uint)(c - 'A') <= ('F' - 'A'));
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsHexLowerChar(int c) public static bool IsHexLowerChar(int c)
{ {
return (uint)(c - '0') <= 9 || (uint)(c - 'a') <= ('f' - 'a'); return unchecked((uint)(c - '0') <= 9 || (uint)(c - 'a') <= ('f' - 'a'));
} }
/// <summary>Map from an ASCII char to its hex value, e.g. arr['b'] == 11. 0xFF means it's not a hex digit.</summary> /// <summary>Map from an ASCII char to its hex value, e.g. arr['b'] == 11. 0xFF means it's not a hex digit.</summary>

View file

@ -159,7 +159,7 @@ public static class StringUtils
private static byte ToLowerAsciiInvariant(byte c) private static byte ToLowerAsciiInvariant(byte c)
{ {
if ((uint)(c - 'A') <= 'Z' - 'A') if (unchecked((uint)(c - 'A') <= 'Z' - 'A'))
{ {
c = (byte)(c | 0x20); c = (byte)(c | 0x20);
} }
@ -240,12 +240,12 @@ public static class StringUtils
public static bool IsAlpha(byte c) public static bool IsAlpha(byte c)
{ {
return (c | 0x20u) - (byte)'a' <= 'z' - 'a'; return unchecked((c | 0x20u) - (byte)'a' <= 'z' - 'a');
} }
public static bool IsDigit(byte c) public static bool IsDigit(byte c)
{ {
return (uint)(c - (byte)'0') <= 9; return unchecked((uint)(c - (byte)'0') <= 9);
} }
public static bool IsHexDigit(byte c) public static bool IsHexDigit(byte c)