From 1e730efed76b024a8811df746e62e56265b98508 Mon Sep 17 00:00:00 2001 From: Melissa Linkert Date: Tue, 11 Aug 2026 11:45:10 -0500 Subject: [PATCH 1/2] TIFF: fix channel unpacking when supplied buffer is too large Fixes #4058. --- .../formats-bsd/src/loci/formats/tiff/TiffParser.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/components/formats-bsd/src/loci/formats/tiff/TiffParser.java b/components/formats-bsd/src/loci/formats/tiff/TiffParser.java index 499cd21e9b2..f0debab62d4 100644 --- a/components/formats-bsd/src/loci/formats/tiff/TiffParser.java +++ b/components/formats-bsd/src/loci/formats/tiff/TiffParser.java @@ -1061,10 +1061,14 @@ public byte[] getSamples(IFD ifd, byte[] buf, int x, int y, } } } + // buf may be larger than the actual image, so don't use buf.length + // to determine the number of bytes per channel + // since offset is updated after every tile copy, it reflects the + // actual number of bytes that were read and copied into buf if (effectiveChannels > 1) { - byte[][] split = new byte[effectiveChannels][buf.length / effectiveChannels]; + byte[][] split = new byte[effectiveChannels][offset / effectiveChannels]; for (int c=0; c Date: Thu, 13 Aug 2026 13:21:30 -0500 Subject: [PATCH 2/2] Add TIFF buffer size tests --- .../test/loci/formats/utests/testng.xml | 7 +++ .../formats/utests/tiff/RGB48TiffMock.java | 60 +++++++++++++++++++ .../formats/utests/tiff/TiffParserTest.java | 14 +++++ 3 files changed, 81 insertions(+) create mode 100644 components/formats-bsd/test/loci/formats/utests/tiff/RGB48TiffMock.java diff --git a/components/formats-bsd/test/loci/formats/utests/testng.xml b/components/formats-bsd/test/loci/formats/utests/testng.xml index 2f2d9369b60..057ea0e5cfe 100644 --- a/components/formats-bsd/test/loci/formats/utests/testng.xml +++ b/components/formats-bsd/test/loci/formats/utests/testng.xml @@ -65,6 +65,13 @@ + + + + + + diff --git a/components/formats-bsd/test/loci/formats/utests/tiff/RGB48TiffMock.java b/components/formats-bsd/test/loci/formats/utests/tiff/RGB48TiffMock.java new file mode 100644 index 00000000000..b2e9109e3f1 --- /dev/null +++ b/components/formats-bsd/test/loci/formats/utests/tiff/RGB48TiffMock.java @@ -0,0 +1,60 @@ +/* + * #%L + * BSD implementations of Bio-Formats readers and writers + * %% + * Copyright (C) 2005 - 2017 Open Microscopy Environment: + * - Board of Regents of the University of Wisconsin-Madison + * - Glencoe Software, Inc. + * - University of Dundee + * %% + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * #L% + */ + +package loci.formats.utests.tiff; + +import java.io.IOException; + +import loci.formats.FormatException; + +public class RGB48TiffMock extends RGBTiffMock { + + public RGB48TiffMock() throws FormatException, IOException { + super(); + } + + @Override + public int[] getBitsPerSample() { + return new int[] { 16, 16, 16}; + } + + @Override + public int[] getRowsPerStrip() { + return new int[] {1}; + } + + @Override + public int[] getStripOffsets() { + return new int[] {0, 36, 72, 108}; + } + +} diff --git a/components/formats-bsd/test/loci/formats/utests/tiff/TiffParserTest.java b/components/formats-bsd/test/loci/formats/utests/tiff/TiffParserTest.java index f839cc15241..d4ade1794e0 100644 --- a/components/formats-bsd/test/loci/formats/utests/tiff/TiffParserTest.java +++ b/components/formats-bsd/test/loci/formats/utests/tiff/TiffParserTest.java @@ -198,5 +198,19 @@ public void testBitsPerSampleMismatch() throws IOException, FormatException { mock.close(); } + @Test + public void testReadPixels() throws IOException, FormatException { + int size = mock.getImageLength(); + int bytesPerPixel = 0; + for (int bps : mock.getBitsPerSample()) { + bytesPerPixel += (bps / 8); + } + int minBuf = size * size * bytesPerPixel; + for (int i=minBuf; i