Skip to content

Commit 61ad962

Browse files
committed
Improve memory limits handeling
DEVSIX-8336
1 parent 01634ed commit 61ad962

File tree

19 files changed

+175
-25
lines changed

19 files changed

+175
-25
lines changed

kernel/src/main/java/com/itextpdf/kernel/colors/WebColors.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public static float[] getCMYKArray(String name) {
248248
color[4] = getAlphaChannelValue(tok.nextToken());
249249
}
250250
}
251-
} catch (Exception exc) {
251+
} catch (Exception e) {
252252
// Will just return null in this case
253253
color = null;
254254
}
@@ -309,7 +309,7 @@ public static float[] getRGBAColor(String name) {
309309
color[1] = (float) (intColor[1] / RGB_MAX_VAL);
310310
color[2] = (float) (intColor[2] / RGB_MAX_VAL);
311311
}
312-
} catch (Exception exc) {
312+
} catch (Exception e) {
313313
// Will just return null in this case
314314
color = null;
315315
}

kernel/src/main/java/com/itextpdf/kernel/crypto/securityhandler/EncryptionUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ static byte[] fetchEnvelopedData(Key certificateKey, Certificate certificate, St
105105
IX509CertificateHolder certHolder;
106106
try {
107107
certHolder = BOUNCY_CASTLE_FACTORY.createX509CertificateHolder(certificate.getEncoded());
108-
} catch (Exception f) {
109-
throw new PdfException(KernelExceptionMessageConstant.PDF_DECRYPTION, f);
108+
} catch (Exception e) {
109+
throw new PdfException(KernelExceptionMessageConstant.PDF_DECRYPTION, e);
110110
}
111111
if (externalDecryptionProcess == null) {
112112
for (int i = 0; i < recipients.size(); i++) {
@@ -125,12 +125,12 @@ static byte[] fetchEnvelopedData(Key certificateKey, Certificate certificate, St
125125
foundRecipient = true;
126126
}
127127
}
128-
} catch (Exception f) {
128+
} catch (Exception e) {
129129
// First check if the feature is supported, it will throw if not
130130
// Exact algorithm doesn't matter currently
131131
BouncyCastleFactoryCreator.getFactory().isEncryptionFeatureSupported(0, true);
132132
// Throw the original exception if the feature is supported
133-
throw new PdfException(KernelExceptionMessageConstant.PDF_DECRYPTION, f);
133+
throw new PdfException(KernelExceptionMessageConstant.PDF_DECRYPTION, e);
134134
}
135135
}
136136
} else {

kernel/src/main/java/com/itextpdf/kernel/font/PdfType3Font.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ private void addGlyphsFromCharProcs(PdfDictionary charProcsDic, int[] widths) {
460460
}
461461
Map<Integer, Integer> unicodeToCode = null;
462462
if (getToUnicode() != null) {
463-
try { unicodeToCode = getToUnicode().createReverseMapping(); } catch (Exception ignored) {}
463+
try { unicodeToCode = getToUnicode().createReverseMapping(); } catch (Exception e) {/*ignored*/}
464464
}
465465

466466
for (PdfName glyphName : charProcsDic.keySet()) {

kernel/src/main/java/com/itextpdf/kernel/pdf/OcgPropertiesCopier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public static void copyOCGProperties(PdfDocument fromDocument, PdfDocument toDoc
6767

6868
OcgPropertiesCopier.copyDDictionary(fromOcgsToCopy, fromOcProperties.getAsDictionary(PdfName.D),
6969
toOcProperties, toDocument);
70-
} catch (Exception ex) {
71-
LOGGER.error(MessageFormatUtil.format(IoLogMessageConstant.OCG_COPYING_ERROR, ex.toString()));
70+
} catch (Exception e) {
71+
LOGGER.error(MessageFormatUtil.format(IoLogMessageConstant.OCG_COPYING_ERROR, e.toString()));
7272
}
7373
}
7474

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfDocument.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This file is part of the iText (R) project.
4141
import com.itextpdf.kernel.events.PdfDocumentEvent;
4242
import com.itextpdf.kernel.exceptions.BadPasswordException;
4343
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
44+
import com.itextpdf.kernel.exceptions.MemoryLimitsAwareException;
4445
import com.itextpdf.kernel.exceptions.PdfException;
4546
import com.itextpdf.kernel.font.PdfFont;
4647
import com.itextpdf.kernel.font.PdfFontFactory;
@@ -1344,11 +1345,11 @@ public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocume
13441345
insertBeforePage += increasingPagesRange.size();
13451346
}
13461347
toDocument.getTagStructureContext().normalizeDocumentRootTag();
1347-
} catch (Exception ex) {
1348+
} catch (Exception e) {
13481349
throw new PdfException(
13491350
KernelExceptionMessageConstant.
13501351
TAG_STRUCTURE_COPYING_FAILED_IT_MIGHT_BE_CORRUPTED_IN_ONE_OF_THE_DOCUMENTS,
1351-
ex);
1352+
e);
13521353
}
13531354
if (copier instanceof IPdfPageFormCopier) {
13541355
((IPdfPageFormCopier) copier).recreateAcroformToProcessCopiedFields(toDocument);
@@ -2385,11 +2386,13 @@ protected void tryInitTagStructure(PdfDictionary str) {
23852386
try {
23862387
structTreeRoot = new PdfStructTreeRoot(str, this);
23872388
structParentIndex = getStructTreeRoot().getParentTreeNextKey();
2388-
} catch (Exception ex) {
2389+
} catch (MemoryLimitsAwareException e){
2390+
throw e;
2391+
} catch (Exception e) {
23892392
structTreeRoot = null;
23902393
structParentIndex = -1;
23912394
Logger logger = LoggerFactory.getLogger(PdfDocument.class);
2392-
logger.error(IoLogMessageConstant.TAG_STRUCTURE_INIT_FAILED, ex);
2395+
logger.error(IoLogMessageConstant.TAG_STRUCTURE_INIT_FAILED, e);
23932396
}
23942397
}
23952398

@@ -2422,9 +2425,11 @@ private void tryFlushTagStructure(boolean isAppendMode) {
24222425
if (!isAppendMode || structTreeRoot.getPdfObject().isModified()) {
24232426
structTreeRoot.flush();
24242427
}
2425-
} catch (Exception ex) {
2428+
} catch (MemoryLimitsAwareException e){
2429+
throw e;
2430+
} catch (Exception e) {
24262431
throw new PdfException(KernelExceptionMessageConstant.TAG_STRUCTURE_FLUSHING_FAILED_IT_MIGHT_BE_CORRUPTED,
2427-
ex);
2432+
e);
24282433
}
24292434
}
24302435

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfPage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,9 +1257,9 @@ void tryFlushPageTags() {
12571257
getDocument().getTagStructureContext().flushPageTags(this);
12581258
}
12591259
getDocument().getStructTreeRoot().savePageStructParentIndexIfNeeded(this);
1260-
} catch (Exception ex) {
1260+
} catch (Exception e) {
12611261
throw new PdfException(
1262-
KernelExceptionMessageConstant.TAG_STRUCTURE_FLUSHING_FAILED_IT_MIGHT_BE_CORRUPTED, ex);
1262+
KernelExceptionMessageConstant.TAG_STRUCTURE_FLUSHING_FAILED_IT_MIGHT_BE_CORRUPTED, e);
12631263
}
12641264
}
12651265

kernel/src/main/java/com/itextpdf/kernel/pdf/PdfReader.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,8 @@ public byte[] readStreamBytesRaw(PdfStream stream) throws IOException {
428428
} finally {
429429
try {
430430
file.close();
431-
} catch (Exception ignored) {
431+
} catch (Exception e) {
432+
// ignored
432433
}
433434
}
434435
return bytes;
@@ -1017,7 +1018,7 @@ protected void readXref() throws IOException {
10171018
| MemoryLimitsAwareException
10181019
| InvalidXRefPrevException exceptionWhileReadingXrefStream) {
10191020
throw exceptionWhileReadingXrefStream;
1020-
} catch (Exception ignored) {
1021+
} catch (Exception e) {
10211022
// Do nothing.
10221023
}
10231024
// clear xref because of possible issues at reading xref stream.
@@ -1363,6 +1364,8 @@ private boolean isCurrentObjectATrailer() {
13631364
try {
13641365
final PdfDictionary dic = (PdfDictionary) readObject(false);
13651366
return dic.get(PdfName.Root, false) != null;
1367+
} catch (MemoryLimitsAwareException e){
1368+
throw e;
13661369
} catch (Exception e) {
13671370
return false;
13681371
}

kernel/src/main/java/com/itextpdf/kernel/pdf/canvas/PdfCanvas.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This file is part of the iText (R) project.
3636
import com.itextpdf.kernel.colors.DeviceGray;
3737
import com.itextpdf.kernel.colors.PatternColor;
3838
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
39+
import com.itextpdf.kernel.exceptions.MemoryLimitsAwareException;
3940
import com.itextpdf.kernel.exceptions.PdfException;
4041
import com.itextpdf.kernel.font.PdfFont;
4142
import com.itextpdf.kernel.font.PdfType0Font;
@@ -2400,8 +2401,10 @@ private PdfStream ensureStreamDataIsReadyToBeProcessed(PdfStream stream) {
24002401
if (stream.getOutputStream() == null || stream.containsKey(PdfName.Filter)) {
24012402
try {
24022403
stream.setData(stream.getBytes());
2403-
} catch (Exception ex) {
2404-
// ignore
2404+
} catch (MemoryLimitsAwareException e){
2405+
throw e;
2406+
} catch (Exception e) {
2407+
// ignored
24052408
}
24062409
}
24072410
}

kernel/src/main/java/com/itextpdf/kernel/pdf/tagging/StandardNamespaces.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public static boolean isHnRole(String role) {
201201
if (role.startsWith("H") && role.length() > 1 && role.charAt(1) != '0') {
202202
try {
203203
return Integer.parseInt(role.substring(1, role.length())) > 0;
204-
} catch (Exception ex) {
204+
} catch (Exception e) {
205205
// ignored
206206
}
207207
}

kernel/src/main/java/com/itextpdf/kernel/utils/CompareTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ public String compareXmp(String outPdf, String cmpPdf, boolean ignoreDateAndProd
802802
if (!compareXmls(cmpBytes, outBytes)) {
803803
return "The XMP packages different!";
804804
}
805-
} catch (Exception ex) {
805+
} catch (Exception e) {
806806
return "XMP parsing failure!";
807807
}
808808
return null;

0 commit comments

Comments
 (0)