@@ -22,8 +22,12 @@ This file is part of the iText (R) project.
22
22
*/
23
23
package com .itextpdf .kernel .pdf ;
24
24
25
+ import com .itextpdf .io .source .ByteArrayOutputStream ;
25
26
import com .itextpdf .kernel .exceptions .KernelExceptionMessageConstant ;
26
27
import com .itextpdf .kernel .exceptions .MemoryLimitsAwareException ;
28
+ import com .itextpdf .kernel .exceptions .PdfException ;
29
+ import com .itextpdf .test .AssertUtil ;
30
+ import com .itextpdf .test .ExceptionTestUtil ;
27
31
import com .itextpdf .test .ExtendedITextTest ;
28
32
import com .itextpdf .test .annotations .type .UnitTest ;
29
33
@@ -128,4 +132,73 @@ public void zeroCapacityInConstructorWithHandlerTest() {
128
132
129
133
Assert .assertEquals (20 , xrefTable .getCapacity ());
130
134
}
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
+ }
131
204
}
0 commit comments