Skip to content

Commit

Permalink
Check ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
wasabii committed Aug 17, 2024
1 parent 52bb104 commit 08d79c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/IKVM.ByteCode/Decoding/LookupSwitchCaseTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ internal static bool TryMeasure(ref SequenceReader<byte> reader, ref int size)
if (reader.TryReadBigEndian(out int npairs) == false)
return false;

if (npairs < 0 || npairs > 16384)
throw new InvalidCodeException("Number of conditions in lookupswitch is out of range.");

var pairsSize = npairs * 8;
size += pairsSize;
if (reader.TryAdvance(pairsSize) == false)
Expand All @@ -99,6 +102,9 @@ internal static bool TryRead(ref SequenceReader<byte> reader, out LookupSwitchCa
if (reader.TryReadBigEndian(out int npairs) == false)
return false;

if (npairs < 0 || npairs > 16384)
throw new InvalidCodeException("Number of conditions in lookupswitch is out of range.");

var pairsSize = npairs * 8;
if (reader.TryReadExact(pairsSize, out var items) == false)
return false;
Expand Down
12 changes: 8 additions & 4 deletions src/IKVM.ByteCode/Decoding/TableSwitchInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ internal static bool TryMeasure(ref SequenceReader<byte> reader, int offset, ref
if (reader.TryReadBigEndian(out int high) == false)
return false;

if (high < low)
throw new InvalidCodeException("High cannot be less than low.");
if (low > high)
throw new InvalidCodeException("Low cannot be greater than high.");
if (high > 16384L + low)
throw new InvalidCodeException("High exceeds maximum size.");

// size of resulting table
if (TableSwitchCaseTable.TryMeasure(ref reader, high - low + 1, ref size) == false)
Expand Down Expand Up @@ -125,8 +127,10 @@ internal static bool TryRead(ref SequenceReader<byte> reader, int offset, out Ta
if (reader.TryReadBigEndian(out int high) == false)
return false;

if (high < low)
throw new InvalidCodeException("High cannot be less than low.");
if (low > high)
throw new InvalidCodeException("Low cannot be greater than high.");
if (high > 16384L + low)
throw new InvalidCodeException("High exceeds maximum size.");

// low value is always 4 bytes
if (TableSwitchCaseTable.TryRead(ref reader, high - low + 1, out var cases) == false)
Expand Down

0 comments on commit 08d79c5

Please sign in to comment.