@@ -22,8 +22,12 @@ This file is part of the iText (R) project.
2222 */
2323package com .itextpdf .kernel .pdf ;
2424
25+ import com .itextpdf .io .source .ByteArrayOutputStream ;
2526import com .itextpdf .kernel .exceptions .KernelExceptionMessageConstant ;
2627import com .itextpdf .kernel .exceptions .MemoryLimitsAwareException ;
28+ import com .itextpdf .kernel .exceptions .PdfException ;
29+ import com .itextpdf .test .AssertUtil ;
30+ import com .itextpdf .test .ExceptionTestUtil ;
2731import com .itextpdf .test .ExtendedITextTest ;
2832import com .itextpdf .test .annotations .type .UnitTest ;
2933
@@ -128,4 +132,73 @@ public void zeroCapacityInConstructorWithHandlerTest() {
128132
129133 Assert .assertEquals (20 , xrefTable .getCapacity ());
130134 }
135+
136+ @ Test
137+ public void xRefMaxValueLong () {
138+ PdfDocument document = new PdfDocument (new PdfWriter (new ByteArrayOutputStream ()));
139+ document .xref .add (new PdfIndirectReferenceProxy (document , 11 , Long .MAX_VALUE ));
140+
141+ Exception e = Assert .assertThrows (PdfException .class , () -> {
142+ document .close ();
143+ });
144+ Assert .assertEquals (KernelExceptionMessageConstant .XREF_HAS_AN_ENTRY_WITH_TOO_BIG_OFFSET , e .getMessage ());
145+ }
146+
147+
148+ @ Test
149+ public void maxCrossReferenceOffSetReached () {
150+ long justOver10gbLogical = 10_000_000_001L ;
151+ PdfDocument document = new PdfDocument (new PdfWriter (new ByteArrayOutputStream ()));
152+ document .xref .add (new PdfIndirectReferenceProxy (document , 11 , justOver10gbLogical ));
153+
154+ Exception e = Assert .assertThrows (PdfException .class , () -> {
155+ document .close ();
156+ });
157+ Assert .assertEquals (KernelExceptionMessageConstant .XREF_HAS_AN_ENTRY_WITH_TOO_BIG_OFFSET , e .getMessage ());
158+ }
159+
160+ @ Test
161+ public void maxCrossReference () {
162+ long justOver10gbLogical = 10_000_000_000L ;
163+ PdfDocument document = new PdfDocument (new PdfWriter (new ByteArrayOutputStream ()));
164+ document .xref .add (new PdfIndirectReferenceProxy (document , 11 , justOver10gbLogical ));
165+
166+ Exception e = Assert .assertThrows (PdfException .class , () -> {
167+ document .close ();
168+ });
169+ Assert .assertEquals (KernelExceptionMessageConstant .XREF_HAS_AN_ENTRY_WITH_TOO_BIG_OFFSET , e .getMessage ());
170+ }
171+
172+ @ Test
173+ public void justBelowXrefThreshold () {
174+ long maxAllowedOffset = 10_000_000_000L - 1L ;
175+ PdfDocument document = new PdfDocument (new PdfWriter (new ByteArrayOutputStream ()));
176+ document .xref .add (new PdfIndirectReferenceProxy (document , 11 , maxAllowedOffset ));
177+
178+ AssertUtil .doesNotThrow (() -> document .close ());
179+ }
180+
181+ @ Test
182+ public void xRefIntMax () {
183+ PdfDocument document = new PdfDocument (new PdfWriter (new ByteArrayOutputStream ()));
184+ document .xref .add (new PdfIndirectReferenceProxy (document , 11 , Integer .MAX_VALUE ));
185+ AssertUtil .doesNotThrow (() -> document .close ());
186+ }
187+
188+
189+
190+
191+ }
192+ class PdfIndirectReferenceProxy extends PdfIndirectReference {
193+ private final long offset ;
194+
195+ public PdfIndirectReferenceProxy (PdfDocument document , int objNumber , long offset ) {
196+ super (document , objNumber );
197+ this .offset = offset ;
198+ }
199+
200+ @ Override
201+ public long getOffset () {
202+ return offset ;
203+ }
131204}
0 commit comments