Skip to content

Guard malformed JPEG/TGA header fields with InvalidImageDataException - #400

Merged
matt-edmondson merged 2 commits into
mainfrom
copilot/repair-jpeg-tga-header-errors
Sep 13, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
copilot/repair-jpeg-tga-header-errors

Conversation

Copilot AI commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

ImageDecoder promises malformed inputs raise InvalidImageDataException, but two unchecked header fields could escape as IndexOutOfRangeException: progressive JPEG spectral selectors and TGA color-map entry depth. This PR adds early validation at parse boundaries so malformed files fail with the documented exception contract.

  • JPEG: validate progressive scan spectral selectors

    • In ReadScan, reject invalid Ss/Se combinations before decode paths index ZigZag[64].
    • Enforced checks:
      • Ss > 63
      • Se > 63
      • Se < Ss
      • Ss == 0 && Se != 0 (spec-constrained DC-only case)
  • TGA: validate color-map entry depth for mapped images

    • For colorMapType == 1, require colorMapEntryBits{8, 15, 16, 24, 32}.
    • Unsupported entry depths now throw InvalidImageDataException before palette decode/indexing.
  • Regression coverage

    • Added a JPEG test mutating a progressive SOS to Ss=64, Se=64, asserting InvalidImageDataException.
    • Added a TGA test with colorMapEntryBits=0 on a color-mapped image, asserting InvalidImageDataException.
if (progressive
	&& (spectralStart > 63
	|| spectralEnd > 63
	|| spectralEnd < spectralStart
	|| (spectralStart == 0 && spectralEnd != 0)))
{
	throw new InvalidImageDataException(
		$"JPEG scan has invalid spectral selection Ss={spectralStart}, Se={spectralEnd}.");
}

Co-authored-by: matt-edmondson <19528727+matt-edmondson@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix IndexOutOfRangeException for malformed JPEG and TGA headers Guard malformed JPEG/TGA header fields with InvalidImageDataException Sep 13, 2026
@matt-edmondson
matt-edmondson marked this pull request as ready for review September 13, 2026 13:04
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Malformed JPEG and TGA headers escape as IndexOutOfRangeException instead of InvalidImageDataException

2 participants