Skip to content

[IMAGING-333] Fix IndexOutOfBoundsException in JpegImageParser.getImageInfo() when no SOF segment is present - #557

Open
akashchamp wants to merge 1 commit into
apache:masterfrom
akashchamp:IMAGING-333-getImageInfo-no-sof
Open

akashchamp wants to merge 1 commit into
apache:masterfrom
akashchamp:IMAGING-333-getImageInfo-no-sof

Conversation

@akashchamp

Copy link
Copy Markdown
  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance.
  • I used AI to create part of this pull request: Claude (Anthropic, via Claude Code) helped locate the root cause, write the one-line fix and the regression test. I reviewed, verified and take responsibility for the change.
  • Ran a successful build using the default Maven goal (mvn).
  • Added a unit test that fails without the fix and passes with it.
  • Wrote a pull request description that explains what/how/why.
  • Each commit has a meaningful subject line and body.

What

JpegImageParser.getImageInfo() throws an unchecked IndexOutOfBoundsException instead of ImagingException when the JPEG has no SOF (Start Of Frame) segment.

Why

readSegments() never returns null; when no SOF marker is present it returns an empty list. getImageInfo() only checked SOF_segments == null before immediately calling SOF_segments.get(0), so the empty-list case fell through to an IndexOutOfBoundsException. The sibling method getImageSize(), a few lines below, already guards against exactly this with abstractSegments == null || abstractSegments.isEmpty(). This PR applies the same guard to getImageInfo().

Fixes IMAGING-333.

How

  • JpegImageParser.getImageInfo(): extend the SOF-segment guard to also check isEmpty(), matching getImageSize(), and throw the existing ImagingException("No SOFN Data Found.") instead.
  • Added JpegWithoutSofSegmentTest, mirroring the existing JpegWithInvalidDhtSegmentTest, with a minimal 4-byte JPEG (SOI + EOI, no segments) asserting Imaging.getImageInfo() throws ImagingException.

Verification

  • Reproduced the reported IndexOutOfBoundsException on current master by running the new test before applying the fix.
  • After the fix, mvn -Dtest=JpegWithoutSofSegmentTest test passes.
  • Ran the full mvn test suite.

…geInfo()

readSegments() never returns null; when a JPEG has no SOF (Start Of
Frame) segment it returns an empty list instead. getImageInfo() only
checked SOF_segments == null before calling SOF_segments.get(0), so
the empty-list case threw an unchecked IndexOutOfBoundsException
rather than the intended ImagingException.

getImageSize(), a few lines below, already guards against this with
`abstractSegments == null || abstractSegments.isEmpty()`. Apply the
same guard to getImageInfo().

Add JpegWithoutSofSegmentTest, mirroring the existing
JpegWithInvalidDhtSegmentTest, asserting that a minimal JPEG with no
SOF segment causes Imaging.getImageInfo() to throw ImagingException.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.

1 participant