Skip to content

Commit fa7cb59

Browse files
committed
PDFBOX-6171: rewrite getDecodeArray() so that enlarged arrays are truncated but keep all the checks
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1932085 13f79535-47bb-0310-9956-ffa450edef68
1 parent 056e849 commit fa7cb59

1 file changed

Lines changed: 23 additions & 27 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/SampledImageReader.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.logging.log4j.Logger;
3434
import org.apache.logging.log4j.LogManager;
3535
import org.apache.pdfbox.cos.COSArray;
36+
import org.apache.pdfbox.cos.COSBase;
3637
import org.apache.pdfbox.cos.COSNumber;
3738
import org.apache.pdfbox.filter.DecodeOptions;
3839
import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
@@ -741,45 +742,40 @@ private static BufferedImage applyColorKeyMask(BufferedImage image, BufferedImag
741742
private static float[] getDecodeArray(PDImage pdImage) throws IOException
742743
{
743744
final COSArray cosDecode = pdImage.getDecode();
744-
float[] decode = null;
745745

746746
if (cosDecode != null)
747747
{
748748
int numberOfComponents = pdImage.getColorSpace().getNumberOfComponents();
749-
if (cosDecode.size() != numberOfComponents * 2)
749+
if (cosDecode.size() >= numberOfComponents * 2)
750750
{
751-
if (pdImage.isStencil() && cosDecode.size() >= 2
752-
&& cosDecode.get(0) instanceof COSNumber
753-
&& cosDecode.get(1) instanceof COSNumber)
751+
boolean error = false;
752+
float[] decode = new float[numberOfComponents * 2];
753+
for (int i = 0; i < decode.length; ++i)
754754
{
755-
float decode0 = ((COSNumber) cosDecode.get(0)).floatValue();
756-
float decode1 = ((COSNumber) cosDecode.get(1)).floatValue();
757-
if (decode0 >= 0 && decode0 <= 1 && decode1 >= 0 && decode1 <= 1)
755+
COSBase base = cosDecode.get(i);
756+
if (base instanceof COSNumber)
758757
{
759-
LOG.warn(
760-
"decode array {} not compatible with color space, using the first two entries",
761-
cosDecode);
762-
return new float[]
763-
{
764-
decode0, decode1
765-
};
758+
decode[i] = ((COSNumber) base).floatValue();
759+
}
760+
else
761+
{
762+
error = true;
763+
break;
766764
}
767765
}
768-
LOG.error("decode array {} not compatible with color space, using default",
769-
cosDecode);
770-
}
771-
else
772-
{
773-
decode = cosDecode.toFloatArray();
766+
if (pdImage.isStencil() && (decode[0] < 0 || decode[0] > 1 || decode[1] < 0 || decode[1] > 1))
767+
{
768+
error = true;
769+
}
770+
if (!error)
771+
{
772+
return decode;
773+
}
774774
}
775775
}
776776

777777
// use color space default
778-
if (decode == null)
779-
{
780-
return pdImage.getColorSpace().getDefaultDecode(pdImage.getBitsPerComponent());
781-
}
782-
783-
return decode;
778+
LOG.error("decode array {} not compatible with color space, using default", cosDecode);
779+
return pdImage.getColorSpace().getDefaultDecode(pdImage.getBitsPerComponent());
784780
}
785781
}

0 commit comments

Comments
 (0)