Author: lehmi
Date: Tue May 19 06:12:25 2026
New Revision: 1934384

Log:
PDFBOX-6192: fixed/extended parameter checks based on a review by Stefan Ziegler

Modified:
   
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
   
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexedTest.java

Modified: 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
    Tue May 19 06:01:18 2026        (r1934383)
+++ 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java
    Tue May 19 06:12:25 2026        (r1934384)
@@ -113,20 +113,30 @@ public final class PDIndexed extends PDS
     public static PDIndexed create(PDColorSpace base, int hival, byte[] 
lookupData)
             throws IOException
     {
-        if (base == null && lookupData == null)
+        if (base == null)
         {
-            throw new IllegalArgumentException("base value is null");
+            throw new IllegalArgumentException("base must not be null");
         }
-        if (base == null && lookupData == null)
+        if (lookupData == null)
         {
-            throw new IllegalArgumentException("lookupData value is null");
+            throw new IllegalArgumentException("lookupData must not be null");
+        }
+        if (hival < 0 || hival > 255)
+        {
+            throw new IllegalArgumentException(" hival has to be a positive 
value <= 255");
+        }
+        int expected = (hival + 1) * base.getNumberOfComponents();
+        if (lookupData.length < expected)
+        {
+            throw new IllegalArgumentException("lookupData too short: expected 
at least " + expected
+                    + " bytes ((hival+1) * components), got " + 
lookupData.length);
         }
         PDIndexed pdIndexed = new PDIndexed();
         pdIndexed.baseColorSpace = base;
         pdIndexed.array.set(1, base.getCOSObject());
         pdIndexed.array.set(2, hival);
         pdIndexed.lookupData = Arrays.copyOf(lookupData, lookupData.length);
-        COSString cosLookupData = new COSString(lookupData, true);
+        COSString cosLookupData = new COSString(pdIndexed.lookupData, true);
         pdIndexed.array.set(3, cosLookupData);
         pdIndexed.readColorTable();
         pdIndexed.initRgbColorTable();

Modified: 
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexedTest.java
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexedTest.java
        Tue May 19 06:01:18 2026        (r1934383)
+++ 
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexedTest.java
        Tue May 19 06:12:25 2026        (r1934384)
@@ -15,7 +15,9 @@
  */
 package org.apache.pdfbox.pdmodel.graphics.color;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
@@ -78,8 +80,53 @@ class PDIndexedTest
         }
         catch (IOException e)
         {
-            fail("Unexpected exception");
+            fail("Unexpected exception", e);
         }
     }
 
+    /**
+     * Test parameter of factory method.
+     */
+    @Test
+    void testFactoryParameterChecks()
+    {
+        final PDColorSpace baseColorspace = PDDeviceRGB.INSTANCE;
+        // empty lookupData as placeholder
+        final byte[] lookupDataEmpty = new byte[5];
+        // define 6 color values
+        final int hival = 5;
+        // create s string containing 6 RGB values. Spaces are added for a 
better readability
+        final String stringLookupData = "AA1166 112233 000000 FEDC01 4561FE 
DC34DA" //
+                .replace(" ", "");
+        byte[] lookupData = null;
+        try
+        {
+            lookupData = COSString.parseHex(stringLookupData).getBytes();
+        }
+        catch (IOException e)
+        {
+            fail("Unexpected exception", e);
+        }
+
+        // check lookupData not null
+        assertThrows(IllegalArgumentException.class,
+                () -> PDIndexed.create(baseColorspace, 0, null));
+        // check base colorspace not null
+        assertThrows(IllegalArgumentException.class,
+                () -> PDIndexed.create(null, 0, lookupDataEmpty));
+        // check hival not negative
+        assertThrows(IllegalArgumentException.class,
+                () -> PDIndexed.create(baseColorspace, -1, lookupDataEmpty));
+        // check hival <= 255
+        assertThrows(IllegalArgumentException.class,
+                () -> PDIndexed.create(baseColorspace, 256, lookupDataEmpty));
+        // check minimum size of lookupData array: (hival + 1) * 
numberOfComponents of base colorspace
+        assertThrows(IllegalArgumentException.class,
+                () -> PDIndexed.create(baseColorspace, hival, 
lookupDataEmpty));
+
+        // everything is fine
+        final byte[] lookupDataOK = lookupData;
+        assertDoesNotThrow(() -> PDIndexed.create(baseColorspace, hival, 
lookupDataOK));
+    }
+
 }

Reply via email to