Martin Mirchev created PDFBOX-6333:
--------------------------------------

             Summary: IndexOutOfBoundsException for an odd-length /Decode array 
in mesh shadings and type 0 functions
                 Key: PDFBOX-6333
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6333
             Project: PDFBox
          Issue Type: Bug
          Components: Rendering
    Affects Versions: 4.0.0, 3.0.8 PDFBox
            Reporter: Martin Mirchev
         Attachments: f0-odd-decode.pdf, sh4-odd-decode.pdf

{{PDTriangleBasedShadingType.getDecodeForParameter()}} and 
{{PDFunctionType0.getDecodeForParameter()}} check {{decodeValues.size() >= 
paramNum * 2 + 1}}, but the returned {{PDRange}} also reads the max value at 
index {{paramNum * 2 + 1}}. With an odd-length /Decode array the last range 
passes the check and {{getMax()}} throws from {{COSArray.getObject()}}, which 
escapes {{PDFRenderer.renderImage()}}:

{code}
java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5
    at org.apache.pdfbox.cos.COSArray.getObject(COSArray.java:249)
{code}

A /Decode array that is even but too short is already handled: 
{{PDShadingType4.collectTriangles()}} throws "Range missing in shading /Decode 
entry".

Attached: {{sh4-odd-decode.pdf}} (type 4 shading, DeviceGray, /Decode [0 255 0 
255 0]) and {{f0-odd-decode.pdf}} (axial shading with a type 0 function, 
/Decode [0]), which both throw; {{sh4-ok.pdf}} and {{f0-ok.pdf}} use complete 
arrays and render. Each result was the same in 3 runs.

A possible change, requiring both values of the range; with it, 
{{sh4-odd-decode.pdf}} gets the existing "Range missing" {{IOException}}, 
{{f0-odd-decode.pdf}} renders using /Range as when /Decode is absent, and the 
two control files are unchanged:

{code}
--- 
a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java
+++ 
b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/PDFunctionType0.java
@@ -216,7 +216,7 @@
     {
         PDRange retval = null;
         COSArray decodeValues = getDecodeValues();
-        if (decodeValues != null && decodeValues.size() >= paramNum * 2 + 1)
+        if (decodeValues != null && decodeValues.size() >= paramNum * 2 + 2)
         {
             retval = new PDRange(decodeValues, paramNum);
         }
--- 
a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDTriangleBasedShadingType.java
+++ 
b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/shading/PDTriangleBasedShadingType.java
@@ -157,7 +157,7 @@
     {
         PDRange retval = null;
         COSArray decodeValues = getDecodeValues();
-        if (decodeValues != null && decodeValues.size() >= paramNum * 2 + 1)
+        if (decodeValues != null && decodeValues.size() >= paramNum * 2 + 2)
         {
             retval = new PDRange(decodeValues, paramNum);
         }
{code}

This issue was found by an LLM-assisted tool and verified manually.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to