Skip to content

Commit 3cf2170

Browse files
committed
Ensure that blocks with all bits set do not throw
1 parent 0a8bae0 commit 3cf2170

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

AssetRipper.TextureDecoder.Tests/AstcTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ private static void AssertCorrectDecompression<T>(int blockWidth, int blockHeigh
7777
}
7878

7979
[TestCase(new byte[16] { 83, 1, 147, 104, 198, 173, 55, 116, 182, 66, 105, 11, 102, 43, 148, 125 }, 8, 8, TestName = "GetBits Should not throw in DecodeIntseq")]
80+
[TestCase(new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 8, 8, TestName = "Zero blocks should not throw", Description = "Blocks of all zeros are valid")]
81+
[TestCase(new byte[16] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, 8, 8, TestName = "0xFF blocks should not throw", Description = "Blocks with all bits set are invalid, but should be handled gracefully")]
8082
public void DecodesBlockWithoutThrowing(byte[] block, int blockWidth, int blockHeight)
8183
{
8284
Assert.DoesNotThrow(() => AstcDecoder.DecodeASTC<ColorBGRA<byte>, byte>(block, blockWidth, blockHeight, blockWidth, blockHeight, out _));

AssetRipper.TextureDecoder/Astc/AstcDecoder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,11 @@ private static ulong BitReverseU64(ulong d, int bits)
883883
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
884884
private static int GetBits(ReadOnlySpan<byte> input, int bit, int len)
885885
{
886+
if (bit < 0 || input.Length * 8 < bit + len)
887+
{
888+
// Invalid input, but we should return something instead of throwing an exception
889+
return default;
890+
}
886891
ReadOnlySpan<byte> slice = input[(bit / 8)..];
887892
int bitBuffer;
888893
if (slice.Length >= sizeof(int))

0 commit comments

Comments
 (0)