Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Commit

Permalink
Subtract don't add, avoid overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jan 20, 2017
1 parent bcac831 commit 7cfade2
Showing 1 changed file with 5 additions and 5 deletions.
Expand Up @@ -249,7 +249,7 @@ public static unsafe void ValidateHeaderCharacters(string headerCharacters)
{
ThrowInvalidHeaderCharacter(pHeader + offset, Vector<byte>.Count);
}
} while (offset + Vector<byte>.Count <= length);
} while (offset <= length - Vector<byte>.Count);
}

// Non-vector testing:
Expand All @@ -258,7 +258,7 @@ public static unsafe void ValidateHeaderCharacters(string headerCharacters)
// Flag > 0x007f => All but highest bit picked up by 0x7f flagging, highest bit picked up by < 0x20 flagging
// Bitwise | or the above three together
// Bitwise & and each char with 0xff80; result should be 0 if all tests pass
if (offset + sizeof(ulong) <= length)
if (offset <= length - sizeof(ulong))
{
do
{
Expand All @@ -268,9 +268,9 @@ public static unsafe void ValidateHeaderCharacters(string headerCharacters)
{
ThrowInvalidHeaderCharacter(pHeader + offset, sizeof(ulong));
}
} while (offset + sizeof(ulong) <= length);
} while (offset <= length - sizeof(ulong));
}
if (offset + sizeof(uint) <= length)
if (offset <= length - sizeof(uint))
{
var stringUint = (uint*)(pHeader + offset);
offset += sizeof(uint);
Expand All @@ -279,7 +279,7 @@ public static unsafe void ValidateHeaderCharacters(string headerCharacters)
ThrowInvalidHeaderCharacter(pHeader + offset, sizeof(uint));
}
}
if (offset + sizeof(ushort) <= length)
if (offset <= length - sizeof(ushort))
{
var stringUshort = (ushort*)(pHeader + offset);
offset += sizeof(ushort);
Expand Down

0 comments on commit 7cfade2

Please sign in to comment.