Skip to content

Commit c47d19e

Browse files
committed
PDFBOX-6192: fixed/extended parameter checks based on a review by
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1934385 13f79535-47bb-0310-9956-ffa450edef68
1 parent ccd281c commit c47d19e

2 files changed

Lines changed: 63 additions & 6 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexed.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ public PDIndexed(COSArray indexedArray, PDResources resources) throws IOExceptio
104104
public static PDIndexed create(PDColorSpace base, int hival, byte[] lookupData)
105105
throws IOException
106106
{
107-
if (base == null && lookupData == null)
107+
if (base == null)
108108
{
109-
throw new IllegalArgumentException("base value is null");
109+
throw new IllegalArgumentException("base must not be null");
110110
}
111-
if (base == null && lookupData == null)
111+
if (lookupData == null)
112+
{
113+
throw new IllegalArgumentException("lookupData must not be null");
114+
}
115+
if (hival < 0 || hival > 255)
116+
{
117+
throw new IllegalArgumentException(" hival has to be a positive value <= 255");
118+
}
119+
int expected = (hival + 1) * base.getNumberOfComponents();
120+
if (lookupData.length < expected)
112121
{
113-
throw new IllegalArgumentException("lookupData value is null");
122+
throw new IllegalArgumentException("lookupData too short: expected at least " + expected
123+
+ " bytes ((hival+1) * components), got " + lookupData.length);
114124
}
115125
PDIndexed pdIndexed = new PDIndexed();
116126
pdIndexed.array = new COSArray();
@@ -119,7 +129,7 @@ public static PDIndexed create(PDColorSpace base, int hival, byte[] lookupData)
119129
pdIndexed.array.add(1, base.getCOSObject());
120130
pdIndexed.array.add(2, COSInteger.get(hival));
121131
pdIndexed.lookupData = Arrays.copyOf(lookupData, lookupData.length);
122-
COSString cosLookupData = new COSString(lookupData, true);
132+
COSString cosLookupData = new COSString(pdIndexed.lookupData, true);
123133
pdIndexed.array.add(3, cosLookupData);
124134
pdIndexed.readColorTable();
125135
pdIndexed.initRgbColorTable();

pdfbox/src/test/java/org/apache/pdfbox/pdmodel/graphics/color/PDIndexedTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.apache.pdfbox.pdmodel.graphics.color;
1717

18+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1819
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
1921
import static org.junit.jupiter.api.Assertions.assertTrue;
2022
import static org.junit.jupiter.api.Assertions.fail;
2123

@@ -78,8 +80,53 @@ void testFactory()
7880
}
7981
catch (IOException e)
8082
{
81-
fail("Unexpected exception");
83+
fail("Unexpected exception", e);
8284
}
8385
}
8486

87+
/**
88+
* Test parameter of factory method.
89+
*/
90+
@Test
91+
void testFactoryParameterChecks()
92+
{
93+
final PDColorSpace baseColorspace = PDDeviceRGB.INSTANCE;
94+
// empty lookupData as placeholder
95+
final byte[] lookupDataEmpty = new byte[5];
96+
// define 6 color values
97+
final int hival = 5;
98+
// create s string containing 6 RGB values. Spaces are added for a better readability
99+
final String stringLookupData = "AA1166 112233 000000 FEDC01 4561FE DC34DA" //
100+
.replace(" ", "");
101+
byte[] lookupData = null;
102+
try
103+
{
104+
lookupData = COSString.parseHex(stringLookupData).getBytes();
105+
}
106+
catch (IOException e)
107+
{
108+
fail("Unexpected exception", e);
109+
}
110+
111+
// check lookupData not null
112+
assertThrows(IllegalArgumentException.class,
113+
() -> PDIndexed.create(baseColorspace, 0, null));
114+
// check base colorspace not null
115+
assertThrows(IllegalArgumentException.class,
116+
() -> PDIndexed.create(null, 0, lookupDataEmpty));
117+
// check hival not negative
118+
assertThrows(IllegalArgumentException.class,
119+
() -> PDIndexed.create(baseColorspace, -1, lookupDataEmpty));
120+
// check hival <= 255
121+
assertThrows(IllegalArgumentException.class,
122+
() -> PDIndexed.create(baseColorspace, 256, lookupDataEmpty));
123+
// check minimum size of lookupData array: (hival + 1) * numberOfComponents of base colorspace
124+
assertThrows(IllegalArgumentException.class,
125+
() -> PDIndexed.create(baseColorspace, hival, lookupDataEmpty));
126+
127+
// everything is fine
128+
final byte[] lookupDataOK = lookupData;
129+
assertDoesNotThrow(() -> PDIndexed.create(baseColorspace, hival, lookupDataOK));
130+
}
131+
85132
}

0 commit comments

Comments
 (0)