Skip to content

Commit 915d8ec

Browse files
ars18wrwiText-CI
authored andcommitted
Throw an exeption in case the catalog cannot be referenced from the trailer
DEVSIX-6548 Autoported commit. Original commit hash: [c26866f30]
1 parent aa1ef56 commit 915d8ec

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

itext.tests/itext.kernel.tests/itext/kernel/pdf/PdfDocumentTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,15 @@ public virtual void MergedSiblingWidgetsRemovePageTest() {
501501
, DESTINATION_FOLDER));
502502
}
503503

504+
[NUnit.Framework.Test]
505+
[LogMessage(iText.IO.Logs.IoLogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT)]
506+
public virtual void RootCannotBeReferenceFromTrailerTest() {
507+
String filename = SOURCE_FOLDER + "rootCannotBeReferenceFromTrailerTest.pdf";
508+
PdfReader corruptedReader = new PdfReader(filename);
509+
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfException), () => new PdfDocument(corruptedReader));
510+
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.CORRUPTED_ROOT_ENTRY_IN_TRAILER, e.Message);
511+
}
512+
504513
private class IgnoreTagStructurePdfDocument : PdfDocument {
505514
internal IgnoreTagStructurePdfDocument(PdfReader reader)
506515
: base(reader) {

itext/itext.kernel/itext/kernel/exceptions/KernelExceptionMessageConstant.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ public const String CONTENT_STREAM_MUST_NOT_INVOKE_OPERATORS_THAT_SPECIFY_COLORS
169169

170170
public const String CORRUPTED_OUTLINE_NO_TITLE_ENTRY = "Document outline is corrupted: some outline (PDF object: \"{0}\") lacks the required title entry.";
171171

172+
public const String CORRUPTED_ROOT_ENTRY_IN_TRAILER = "The trailer is corrupted: the catalog " + "is corrupted or cannot be referenced from the file's trailer. The PDF cannot be opened.";
173+
172174
public const String DATA_HANDLER_COUNTER_HAS_BEEN_DISABLED = "Data handler counter has been disabled";
173175

174176
public const String DEFAULT_CRYPT_FILTER_NOT_FOUND_ENCRYPTION = "/DefaultCryptFilter not found " + "(encryption).";

itext/itext.kernel/itext/kernel/pdf/PdfDocument.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2016,7 +2016,11 @@ protected internal virtual void Open(PdfVersion newPdfVersion) {
20162016
pdfVersion = reader.headerPdfVersion;
20172017
trailer = new PdfDictionary(reader.trailer);
20182018
ReadDocumentIds();
2019-
catalog = new PdfCatalog((PdfDictionary)trailer.Get(PdfName.Root, true));
2019+
PdfDictionary catalogDictionary = (PdfDictionary)trailer.Get(PdfName.Root, true);
2020+
if (null == catalogDictionary) {
2021+
throw new PdfException(KernelExceptionMessageConstant.CORRUPTED_ROOT_ENTRY_IN_TRAILER);
2022+
}
2023+
catalog = new PdfCatalog(catalogDictionary);
20202024
UpdatePdfVersionFromCatalog();
20212025
PdfStream xmpMetadataStream = catalog.GetPdfObject().GetAsStream(PdfName.Metadata);
20222026
if (xmpMetadataStream != null) {

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1953b3f0519c059fb438066102e090c396a051a4
1+
c26866f3001f1a3604045fe2eb063e5f5d4e9ebb

0 commit comments

Comments
 (0)