Skip to content

Commit 42c37f4

Browse files
author
Maruan Sahyoun
committed
PDFBOX-6384: Calculate the predictor row length without int overflow
Co-Authored-By: Claude <noreply@anthropic.com> git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1941380 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7ceb9ba commit 42c37f4

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

‎pdfbox/src/main/java/org/apache/pdfbox/filter/Predictor.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,15 @@ else if (absb <= absc)
201201
}
202202
}
203203

204-
static int calculateRowLength(int colors, int bitsPerComponent, int columns)
204+
static long calculateRowLength(int colors, int bitsPerComponent, int columns)
205205
{
206-
final int bitsPerPixel = colors * bitsPerComponent;
207-
return (columns * bitsPerPixel + 7) / 8;
206+
if (colors <= 0 || bitsPerComponent <= 0 || columns <= 0)
207+
{
208+
return 0;
209+
}
210+
// long to avoid an overflow to a small row length with huge parameters
211+
final long bitsPerPixel = (long) colors * bitsPerComponent;
212+
return (columns * bitsPerPixel + 7) / 8;
208213
}
209214

210215
// get value from bit interval from a byte
@@ -284,12 +289,12 @@ private static final class PredictorOutputStream extends FilterOutputStream
284289
this.colors = colors;
285290
this.bitsPerComponent = bitsPerComponent;
286291
this.columns = columns;
287-
this.rowLength = calculateRowLength(colors, bitsPerComponent, columns);
288-
if (rowLength <= 0)
292+
long length = calculateRowLength(colors, bitsPerComponent, columns);
293+
if (length <= 0)
289294
{
290295
// A zero row length (e.g. /Columns 0) would leave write() making no progress,
291296
// a non-terminating loop for non-PNG predictors; reject it alongside negatives.
292-
throw new IOException("Calculated row length is not positive: " + rowLength);
297+
throw new IOException("Calculated row length is not positive: " + length);
293298
}
294299
// PDFBOX-6265: prevent OOM with extreme values
295300
long maxRowLength = 10_000_000L;
@@ -310,14 +315,15 @@ private static final class PredictorOutputStream extends FilterOutputStream
310315
// ignore invalid value, keep default
311316
}
312317
}
313-
if (rowLength > maxRowLength)
318+
if (length > maxRowLength)
314319
{
315320
String msg =
316321
String.format("Calculated row length is too high: %d "
317322
+ "(colors: %d, bitsPerComponent: %d, columns: %d)",
318-
rowLength, colors, bitsPerComponent, columns);
323+
length, colors, bitsPerComponent, columns);
319324
throw new IOException(msg);
320325
}
326+
this.rowLength = (int) length;
321327
this.predictorPerRow = predictor >= 10;
322328
currentRow = new byte[rowLength];
323329
lastRow = new byte[rowLength];

‎pdfbox/src/test/java/org/apache/pdfbox/filter/PredictorTest.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,48 @@ void testNonPositiveRowLengthRejected()
105105
decodeParams.setInt(COSName.COLUMNS, 0);
106106
assertThrows(IOException.class, () -> Predictor.wrapPredictor(new ByteArrayOutputStream(), decodeParams));
107107
}
108+
109+
/**
110+
* A row length above the limit must be rejected before the row buffers are allocated.
111+
*/
112+
@Test
113+
void testTooHighRowLengthRejected()
114+
{
115+
COSDictionary decodeParams = createDecodeParams(1, 8, 20_000_000);
116+
assertThrows(IOException.class,
117+
() -> Predictor.wrapPredictor(new ByteArrayOutputStream(), decodeParams));
118+
}
119+
120+
/**
121+
* Parameters whose product overflows an int must not result in a small row length.
122+
*/
123+
@Test
124+
void testOverflowingRowLengthRejected()
125+
{
126+
// 536870913 * 8 bits overflow to 8 bits, i.e. a row length of 1
127+
COSDictionary decodeParams = createDecodeParams(1, 8, 536_870_913);
128+
assertThrows(IOException.class,
129+
() -> Predictor.wrapPredictor(new ByteArrayOutputStream(), decodeParams));
130+
}
131+
132+
/**
133+
* Negative /Colors and /BitsPerComponent must not result in a positive row length.
134+
*/
135+
@Test
136+
void testNegativeParametersRejected()
137+
{
138+
COSDictionary decodeParams = createDecodeParams(-1, -8, 1);
139+
assertThrows(IOException.class,
140+
() -> Predictor.wrapPredictor(new ByteArrayOutputStream(), decodeParams));
141+
}
142+
143+
private static COSDictionary createDecodeParams(int colors, int bitsPerComponent, int columns)
144+
{
145+
COSDictionary decodeParams = new COSDictionary();
146+
decodeParams.setInt(COSName.PREDICTOR, 2);
147+
decodeParams.setInt(COSName.COLORS, colors);
148+
decodeParams.setInt(COSName.BITS_PER_COMPONENT, bitsPerComponent);
149+
decodeParams.setInt(COSName.COLUMNS, columns);
150+
return decodeParams;
151+
}
108152
}

0 commit comments

Comments
 (0)