Skip to content

Commit 1461f75

Browse files
joris.schellekensyulian-gaponenko
authored andcommitted
add another edge-case to the code that reads xref tables
add test resolves DEVSIX-1426 Autoported commit. Original commit hash: [1b98844b04]
1 parent 9119ac7 commit 1461f75

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using iText.Kernel.Utils;
3+
using iText.Test;
4+
5+
namespace iText.Kernel.Pdf {
6+
/// <summary>
7+
/// This test checks correct handling of pdf documents with (slightly) corrupt XREF table
8+
/// xref
9+
/// 0 30
10+
/// 0000000000 65535 f
11+
/// 0000000000 65535 f
12+
/// </summary>
13+
public class CorruptXRefTest {
14+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
15+
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/CorruptXRefTest/";
16+
17+
public static readonly String outputFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory + "/test/itext/kernel/pdf/CorruptXRefTest/";
18+
19+
[NUnit.Framework.OneTimeSetUp]
20+
public static void BeforeClass() {
21+
ITextTest.CreateDestinationFolder(outputFolder);
22+
}
23+
24+
/// <exception cref="System.IO.IOException"/>
25+
/// <exception cref="System.Exception"/>
26+
[NUnit.Framework.Test]
27+
public virtual void ReadPdfWithCorruptXRef() {
28+
String cmpFile = sourceFolder + "cmp_docOut.pdf";
29+
String outputFile = outputFolder + "docOut.pdf";
30+
String inputFile = sourceFolder + "docIn.pdf";
31+
PdfWriter writer = new PdfWriter(outputFile);
32+
PdfReader reader = new PdfReader(inputFile);
33+
PdfDocument inputPdfDocument = new PdfDocument(reader);
34+
PdfDocument outputPdfDocument = new PdfDocument(writer);
35+
int lastPage = inputPdfDocument.GetNumberOfPages();
36+
inputPdfDocument.CopyPagesTo(lastPage, lastPage, outputPdfDocument);
37+
inputPdfDocument.Close();
38+
outputPdfDocument.Close();
39+
NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outputFolder + "docOut.pdf", cmpFile, outputFolder
40+
, "diff_"));
41+
}
42+
}
43+
}
Binary file not shown.
Binary file not shown.

itext/itext.kernel/itext/kernel/pdf/PdfReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ protected internal virtual PdfDictionary ReadXrefSection() {
891891
tokens.NextValidToken();
892892
int gen = tokens.GetIntValue();
893893
tokens.NextValidToken();
894-
if (pos == 0L && gen == 65535 && num == 1) {
894+
if (pos == 0L && gen == 65535 && num == 1 && start != 0) {
895895
// Very rarely can an XREF have an incorrect start number. (SUP-1557)
896896
// e.g.
897897
// xref

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
28bdf7fff3502700d5093408fa91d35c180e5973
1+
1b98844b04ed0e75883e0a70706d8f36bbbf932d

0 commit comments

Comments
 (0)