Skip to content

Commit 696df73

Browse files
committed
Implement memory limits aware handler. Filter some SpotBugs expections.
DEVSIX-2856
1 parent 7d8ade0 commit 696df73

23 files changed

+1307
-32
lines changed

kernel/findbugs-filter.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,4 +470,14 @@
470470
<Method name="open"/>
471471
<Bug pattern="PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"/>
472472
</Match>
473+
<Match>
474+
<Class name="com.itextpdf.kernel.pdf.filters.FlateDecodeFilter"/>
475+
<Method name="flateDecode"/>
476+
<Bug pattern="EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS"/>
477+
</Match>
478+
<Match>
479+
<Class name="com.itextpdf.kernel.pdf.filters.FlateDecodeStrictFilter"/>
480+
<Method name="flateDecode"/>
481+
<Bug pattern="EXS_EXCEPTION_SOFTENING_NO_CONSTRAINTS"/>
482+
</Match>
473483
</FindBugsFilter>

kernel/src/main/java/com/itextpdf/kernel/PdfException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public class PdfException extends RuntimeException {
144144
public static final String DocumentHasNoPdfCatalogObject = "Document has no PDF Catalog object.";
145145
public static final String DocumentMustBePreClosed = "Document must be preClosed.";
146146
public static final String DocumentForCopyToCannotBeNull = "Document for copyTo cannot be null.";
147+
public static final String DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed = "During decompression multiple streams in sum occupied more memory than allowed. Please either check your pdf or increase the allowed single decompressed pdf stream maximum size value by setting the appropriate parameter of ReaderProperties's MemoryLimitsAwareHandler.";
148+
public static final String DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed = "During decompression a single stream occupied more memory than allowed. Please either check your pdf or increase the allowed multiple decompressed pdf streams maximum size value by setting the appropriate parameter of ReaderProperties's MemoryLimitsAwareHandler.";
149+
public static final String DuringDecompressionSingleStreamOccupiedMoreThanMaxIntegerValue = "During decompression a single stream occupied more than a maximum integer value. Please check your pdf.";
147150
public static final String EndOfContentStreamReachedBeforeEndOfImageData = "End of content stream reached before end of image data.";
148151
public static final String ErrorWhileReadingObjectStream = "Error while reading Object Stream.";
149152
public static final String EncryptedPayloadFileSpecDoesntHaveEncryptedPayloadDictionary = "Encrypted payload file spec shall have encrypted payload dictionary.";
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf;
44+
45+
import com.itextpdf.kernel.PdfException;
46+
47+
/**
48+
* Exception class for exceptions occurred during decompressed pdf streams processing.
49+
*/
50+
public class MemoryLimitsAwareException extends PdfException {
51+
/**
52+
* Creates a new instance of MemoryLimitsAwareException.
53+
*
54+
* @param message the detail message.
55+
*/
56+
public MemoryLimitsAwareException(String message) {
57+
super(message);
58+
}
59+
60+
/**
61+
* Creates a new instance of MemoryLimitsAwareException.
62+
*
63+
* @param cause the cause (which is saved for later retrieval by {@link #getCause()} method).
64+
*/
65+
public MemoryLimitsAwareException(Throwable cause) {
66+
this(UnknownPdfException, cause);
67+
}
68+
69+
/**
70+
* Creates a new instance of MemoryLimitsAwareException.
71+
*
72+
* @param message the detail message.
73+
* @param cause the cause (which is saved for later retrieval by {@link #getCause()} method).
74+
*/
75+
public MemoryLimitsAwareException(String message, Throwable cause) {
76+
super(message, cause);
77+
}
78+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf;
44+
45+
import com.itextpdf.kernel.pdf.filters.IFilterHandler;
46+
47+
import java.io.ByteArrayOutputStream;
48+
49+
/**
50+
* Handles memory limits aware processing.
51+
*
52+
* @see {@link MemoryLimitsAwareHandler}
53+
*/
54+
public abstract class MemoryLimitsAwareFilter implements IFilterHandler {
55+
56+
/**
57+
* Creates a {@link MemoryLimitsAwareOutputStream} which will be used for decompression of the passed pdf stream.
58+
*
59+
* @param streamDictionary the pdf stream which is going to be decompressed.
60+
* @return the {@link ByteArrayOutputStream} which will be used for decompression of the passed pdf stream
61+
*/
62+
public ByteArrayOutputStream enableMemoryLimitsAwareHandler(PdfDictionary streamDictionary) {
63+
MemoryLimitsAwareOutputStream outputStream = new MemoryLimitsAwareOutputStream();
64+
MemoryLimitsAwareHandler memoryLimitsAwareHandler = null;
65+
if (null != streamDictionary.getIndirectReference()) {
66+
memoryLimitsAwareHandler = streamDictionary.getIndirectReference().getDocument().memoryLimitsAwareHandler;
67+
} else {
68+
// We do not reuse some static instance because one can process pdfs in different threads.
69+
memoryLimitsAwareHandler = new MemoryLimitsAwareHandler();
70+
}
71+
if (null != memoryLimitsAwareHandler && memoryLimitsAwareHandler.considerCurrentPdfStream) {
72+
outputStream.setMaxStreamSize(memoryLimitsAwareHandler.getMaxSizeOfSingleDecompressedPdfStream());
73+
}
74+
return outputStream;
75+
}
76+
}
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2019 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.kernel.pdf;
44+
45+
import com.itextpdf.kernel.PdfException;
46+
47+
import java.io.Serializable;
48+
49+
/**
50+
* A {@link MemoryLimitsAwareHandler} handles memory allocation and prevents decompressed pdf streams from occupation of more space than allowed.
51+
*/
52+
public class MemoryLimitsAwareHandler implements Serializable {
53+
54+
private static final long serialVersionUID = 2499046471291214639L;
55+
56+
private static final int SINGLE_SCALE_COEFFICIENT = 100;
57+
private static final int SUM_SCALE_COEFFICIENT = 500;
58+
59+
private static final int SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE = Integer.MAX_VALUE / 100;
60+
private static final long SUM_OF_DECOMPRESSED_PDF_STREAMW_MIN_SIZE = Integer.MAX_VALUE / 20;
61+
62+
private int maxSizeOfSingleDecompressedPdfStream;
63+
private long maxSizeOfDecompressedPdfStreamsSum;
64+
65+
private long allMemoryUsedForDecompression = 0;
66+
private long memoryUsedForCurrentPdfStreamDecompression = 0;
67+
68+
boolean considerCurrentPdfStream = false;
69+
70+
/**
71+
* Creates a {@link MemoryLimitsAwareHandler} which will be used to handle decompression of pdf streams.
72+
* The max allowed memory limits will be generated by default.
73+
*/
74+
public MemoryLimitsAwareHandler() {
75+
maxSizeOfSingleDecompressedPdfStream = SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE;
76+
maxSizeOfDecompressedPdfStreamsSum = SUM_OF_DECOMPRESSED_PDF_STREAMW_MIN_SIZE;
77+
}
78+
79+
/**
80+
* Creates a {@link MemoryLimitsAwareHandler} which will be used to handle decompression of pdf streams.
81+
* The max allowed memory limits will be generated by default, based on the size of the document.
82+
*
83+
* @param documentSize the size of the document, which is going to be handled by iText.
84+
*/
85+
public MemoryLimitsAwareHandler(long documentSize) {
86+
maxSizeOfSingleDecompressedPdfStream = (int) calculateDefaultParameter(documentSize, SINGLE_SCALE_COEFFICIENT, SINGLE_DECOMPRESSED_PDF_STREAM_MIN_SIZE);
87+
maxSizeOfDecompressedPdfStreamsSum = calculateDefaultParameter(documentSize, SUM_SCALE_COEFFICIENT, SUM_OF_DECOMPRESSED_PDF_STREAMW_MIN_SIZE);
88+
}
89+
90+
/**
91+
* Gets the maximum allowed size which can be occupied by a single decompressed pdf stream.
92+
*
93+
* @return the maximum allowed size which can be occupied by a single decompressed pdf stream.
94+
*/
95+
public int getMaxSizeOfSingleDecompressedPdfStream() {
96+
return maxSizeOfSingleDecompressedPdfStream;
97+
}
98+
99+
/**
100+
* Sets the maximum allowed size which can be occupied by a single decompressed pdf stream.
101+
* This value correlates with maximum heap size. This value should not exceed limit of the heap size.
102+
*
103+
* iText will throw an exception if during decompression a pdf stream with two or more filters of identical type
104+
* occupies more memory than allowed.
105+
*
106+
* @param maxSizeOfSingleDecompressedPdfStream the maximum allowed size which can be occupied by a single decompressed pdf stream.
107+
* @return this {@link MemoryLimitsAwareHandler} instance.
108+
*/
109+
public MemoryLimitsAwareHandler setMaxSizeOfSingleDecompressedPdfStream(int maxSizeOfSingleDecompressedPdfStream) {
110+
this.maxSizeOfSingleDecompressedPdfStream = maxSizeOfSingleDecompressedPdfStream;
111+
return this;
112+
}
113+
114+
/**
115+
* Gets the maximum allowed size which can be occupied by all decompressed pdf streams.
116+
*
117+
* @return the maximum allowed size value which streams may occupy
118+
*/
119+
public long getMaxSizeOfDecompressedPdfStreamsSum() {
120+
return maxSizeOfDecompressedPdfStreamsSum;
121+
}
122+
123+
/**
124+
* Sets the maximum allowed size which can be occupied by all decompressed pdf streams.
125+
* This value can be limited by the maximum expected PDF file size when it's completely decompressed.
126+
* Setting this value correlates with the maximum processing time spent on document reading
127+
*
128+
* iText will throw an exception if during decompression pdf streams with two or more filters of identical type
129+
* occupy more memory than allowed.
130+
*
131+
* @param maxSizeOfDecompressedPdfStreamsSum he maximum allowed size which can be occupied by all decompressed pdf streams.
132+
* @return this {@link MemoryLimitsAwareHandler} instance.
133+
*/
134+
public MemoryLimitsAwareHandler setMaxSizeOfDecompressedPdfStreamsSum(long maxSizeOfDecompressedPdfStreamsSum) {
135+
this.maxSizeOfDecompressedPdfStreamsSum = maxSizeOfDecompressedPdfStreamsSum;
136+
return this;
137+
}
138+
139+
/**
140+
* Considers the number of bytes which are occupied by the decompressed pdf stream.
141+
* If memory limits have not been faced, throws an exception.
142+
*
143+
* @param numOfOccupiedBytes the number of bytes which are occupied by the decompressed pdf stream.
144+
* @return this {@link MemoryLimitsAwareHandler} instance.
145+
* @see {@link MemoryLimitsAwareException}
146+
*/
147+
MemoryLimitsAwareHandler considerBytesOccupiedByDecompressedPdfStream(long numOfOccupiedBytes) {
148+
if (considerCurrentPdfStream && memoryUsedForCurrentPdfStreamDecompression < numOfOccupiedBytes) {
149+
memoryUsedForCurrentPdfStreamDecompression = numOfOccupiedBytes;
150+
if (memoryUsedForCurrentPdfStreamDecompression > maxSizeOfSingleDecompressedPdfStream) {
151+
throw new MemoryLimitsAwareException(PdfException.DuringDecompressionSingleStreamOccupiedMoreMemoryThanAllowed);
152+
}
153+
}
154+
return this;
155+
}
156+
157+
/**
158+
* Begins handling of current pdf stream decompression.
159+
*
160+
* @return this {@link MemoryLimitsAwareHandler} instance.
161+
*/
162+
MemoryLimitsAwareHandler beginDecompressedPdfStreamProcessing() {
163+
ensureCurrentStreamIsReset();
164+
considerCurrentPdfStream = true;
165+
return this;
166+
}
167+
168+
/**
169+
* Ends handling of current pdf stream decompression.
170+
* If memory limits have not been faced, throws an exception.
171+
*
172+
* @return this {@link MemoryLimitsAwareHandler} instance.
173+
* @see {@link MemoryLimitsAwareException}
174+
*/
175+
MemoryLimitsAwareHandler endDecompressedPdfStreamProcessing() {
176+
allMemoryUsedForDecompression += memoryUsedForCurrentPdfStreamDecompression;
177+
if (allMemoryUsedForDecompression > maxSizeOfDecompressedPdfStreamsSum) {
178+
throw new MemoryLimitsAwareException(PdfException.DuringDecompressionMultipleStreamsInSumOccupiedMoreMemoryThanAllowed);
179+
}
180+
ensureCurrentStreamIsReset();
181+
considerCurrentPdfStream = false;
182+
return this;
183+
}
184+
185+
long getAllMemoryUsedForDecompression() {
186+
return allMemoryUsedForDecompression;
187+
}
188+
189+
private static long calculateDefaultParameter(long documentSize, int scale, long min) {
190+
long result = documentSize * scale;
191+
if (result < min) {
192+
result = min;
193+
}
194+
if (result > min * scale) {
195+
result = min * scale;
196+
}
197+
return result;
198+
}
199+
200+
private void ensureCurrentStreamIsReset() {
201+
memoryUsedForCurrentPdfStreamDecompression = 0;
202+
}
203+
}

0 commit comments

Comments
 (0)