Skip to content

Commit 1b98844

Browse files
author
joris.schellekens
committed
add another edge-case to the code that reads xref tables
add test resolves DEVSIX-1426
1 parent 28bdf7f commit 1b98844

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ protected PdfDictionary readXrefSection() throws IOException {
808808
tokens.nextValidToken();
809809
int gen = tokens.getIntValue();
810810
tokens.nextValidToken();
811-
if (pos == 0L && gen == 65535 && num == 1) {
811+
if (pos == 0L && gen == 65535 && num == 1 && start != 0) {
812812
// Very rarely can an XREF have an incorrect start number. (SUP-1557)
813813
// e.g.
814814
// xref
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.itextpdf.kernel.pdf;
2+
3+
import com.itextpdf.kernel.utils.CompareTool;
4+
import com.itextpdf.test.annotations.type.IntegrationTest;
5+
import org.junit.Assert;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
import org.junit.experimental.categories.Category;
9+
10+
import java.io.IOException;
11+
12+
import static com.itextpdf.test.ITextTest.createDestinationFolder;
13+
14+
/**
15+
* This test checks correct handling of pdf documents with (slightly) corrupt XREF table
16+
* xref
17+
* 0 30
18+
* 0000000000 65535 f
19+
* 0000000000 65535 f
20+
*/
21+
@Category(IntegrationTest.class)
22+
public class CorruptXRefTest {
23+
24+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/CorruptXRefTest/";
25+
public static final String outputFolder = "./target/test/com/itextpdf/kernel/pdf/CorruptXRefTest/";
26+
27+
@BeforeClass
28+
public static void beforeClass() {
29+
createDestinationFolder(outputFolder);
30+
}
31+
32+
@Test
33+
public void readPdfWithCorruptXRef() throws IOException, InterruptedException {
34+
String cmpFile = sourceFolder + "cmp_docOut.pdf";
35+
String outputFile = outputFolder + "docOut.pdf";
36+
String inputFile = sourceFolder + "docIn.pdf";
37+
PdfWriter writer = new PdfWriter(outputFile);
38+
39+
PdfReader reader = new PdfReader(inputFile);
40+
41+
PdfDocument inputPdfDocument = new PdfDocument(reader);
42+
PdfDocument outputPdfDocument = new PdfDocument(writer);
43+
44+
int lastPage = inputPdfDocument.getNumberOfPages();
45+
inputPdfDocument.copyPagesTo(lastPage, lastPage, outputPdfDocument);
46+
47+
inputPdfDocument.close();
48+
outputPdfDocument.close();
49+
50+
Assert.assertNull(new CompareTool().compareByContent(outputFolder + "docOut.pdf", cmpFile, outputFolder, "diff_"));
51+
52+
}
53+
}
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)