Reject stalled BZip2 decoder states after corruption

This commit is contained in:
Adam Hathcock
2026-11-09 11:16:14 +00:00
parent 54432058de
commit fa3dff056a
5 changed files with 217 additions and 14 deletions

View File

@@ -21,26 +21,24 @@ internal partial class CBZip2InputStream
var retChar = currentChar;
switch (currentState)
{
case START_BLOCK_STATE:
break;
case RAND_PART_A_STATE:
break;
case RAND_PART_B_STATE:
await SetupRandPartBAsync(cancellationToken).ConfigureAwait(false);
break;
case RAND_PART_C_STATE:
await SetupRandPartCAsync(cancellationToken).ConfigureAwait(false);
break;
case NO_RAND_PART_A_STATE:
break;
case NO_RAND_PART_B_STATE:
await SetupNoRandPartBAsync(cancellationToken).ConfigureAwait(false);
break;
case NO_RAND_PART_C_STATE:
await SetupNoRandPartCAsync(cancellationToken).ConfigureAwait(false);
break;
case START_BLOCK_STATE:
case RAND_PART_A_STATE:
case NO_RAND_PART_A_STATE:
default:
break;
// Match ReadByte(): retries after a decoding error must not generate endless output.
throw new InvalidFormatException("BZip2: invalid decoder state");
}
return retChar;
}

View File

@@ -263,26 +263,25 @@ internal partial class CBZip2InputStream : Stream
var retChar = currentChar;
switch (currentState)
{
case START_BLOCK_STATE:
break;
case RAND_PART_A_STATE:
break;
case RAND_PART_B_STATE:
SetupRandPartB();
break;
case RAND_PART_C_STATE:
SetupRandPartC();
break;
case NO_RAND_PART_A_STATE:
break;
case NO_RAND_PART_B_STATE:
SetupNoRandPartB();
break;
case NO_RAND_PART_C_STATE:
SetupNoRandPartC();
break;
case START_BLOCK_STATE:
case RAND_PART_A_STATE:
case NO_RAND_PART_A_STATE:
default:
break;
// These transitional states can remain after a decoding error. A retry (including
// entry disposal) must fail rather than return the same byte indefinitely.
throw new InvalidFormatException("BZip2: invalid decoder state");
}
return retChar;
}