From 5a66d151dd5c1dd99865ed4a218ba1d4bd4d0c57 Mon Sep 17 00:00:00 2001 From: Akash Kumar <116457960+akashchamp@users.noreply.github.com> Date: Fri, 25 Sep 2026 22:57:44 +0530 Subject: [PATCH] [IMAGING-333] Fix IndexOutOfBoundsException in JpegImageParser.getImageInfo() 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 --- .../imaging/formats/jpeg/JpegImageParser.java | 2 +- .../jpeg/JpegWithoutSofSegmentTest.java | 41 +++++++++++++++++++ .../resources/IMAGING-333/NoSofSegment.jpg | 1 + 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/commons/imaging/formats/jpeg/JpegWithoutSofSegmentTest.java create mode 100644 src/test/resources/IMAGING-333/NoSofSegment.jpg diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java index 4dee3e7e64..d4999a01d3 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java @@ -342,7 +342,7 @@ public ImageInfo getImageInfo(final ByteSource byteSource, final JpegImagingPara }, false); - if (SOF_segments == null) { + if (SOF_segments == null || SOF_segments.isEmpty()) { throw new ImagingException("No SOFN Data Found."); } diff --git a/src/test/java/org/apache/commons/imaging/formats/jpeg/JpegWithoutSofSegmentTest.java b/src/test/java/org/apache/commons/imaging/formats/jpeg/JpegWithoutSofSegmentTest.java new file mode 100644 index 0000000000..fd666fe118 --- /dev/null +++ b/src/test/java/org/apache/commons/imaging/formats/jpeg/JpegWithoutSofSegmentTest.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.imaging.formats.jpeg; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.File; + +import org.apache.commons.imaging.Imaging; +import org.apache.commons.imaging.ImagingException; +import org.apache.commons.imaging.test.TestResources; +import org.junit.jupiter.api.Test; + +/** + * Tests that a JPEG file without an SOF (Start Of Frame) segment causes {@link Imaging#getImageInfo(File)} to throw an + * {@link ImagingException} instead of an unchecked {@link IndexOutOfBoundsException}. + */ +class JpegWithoutSofSegmentTest { + + @Test + void testGetImageInfo() { + // we cannot use ImagingTest and getImageByFileName, as it would cause other tests to fail + final File imageFile = TestResources.resourceToFile("/IMAGING-333/NoSofSegment.jpg"); + assertThrows(ImagingException.class, () -> Imaging.getImageInfo(imageFile)); + } +} diff --git a/src/test/resources/IMAGING-333/NoSofSegment.jpg b/src/test/resources/IMAGING-333/NoSofSegment.jpg new file mode 100644 index 0000000000..6bd169662e --- /dev/null +++ b/src/test/resources/IMAGING-333/NoSofSegment.jpg @@ -0,0 +1 @@ +ÿØÿÙ \ No newline at end of file