Skip to content

Commit

Permalink
Fix ArrayIndexOutOfBoundsException while opening invalid document wit…
Browse files Browse the repository at this point in the history
…h huge object reference

DEVSIX-7948

Autoported commit.
Original commit hash: [0ce21161e]
  • Loading branch information
introfog authored and iText-CI committed Dec 6, 2023
1 parent b9c8e4c commit 8ff2cbe
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,56 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using iText.IO.Source;
using iText.Kernel.Exceptions;
using iText.Kernel.Utils;
using iText.Test;
using iText.Test.Attributes;

namespace iText.Kernel.Pdf {
[NUnit.Framework.Category("IntegrationTest")]
public class PdfXrefTableTest : ExtendedITextTest {
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
public static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
.CurrentContext.TestDirectory) + "/resources/itext/kernel/pdf/PdfXrefTableTest/";

public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
public static readonly String DESTINATION_FOLDER = NUnit.Framework.TestContext.CurrentContext.TestDirectory
+ "/test/itext/kernel/pdf/PdfXrefTableTest/";

[NUnit.Framework.OneTimeSetUp]
public static void BeforeClass() {
CreateOrClearDestinationFolder(destinationFolder);
CreateOrClearDestinationFolder(DESTINATION_FOLDER);
}

[NUnit.Framework.OneTimeTearDown]
public static void AfterClass() {
CompareTool.Cleanup(destinationFolder);
CompareTool.Cleanup(DESTINATION_FOLDER);
}

[NUnit.Framework.Test]
[LogMessage(iText.IO.Logs.IoLogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT, LogLevel =
LogLevelConstants.ERROR)]
public virtual void OpenInvalidDocWithHugeRefTest() {
String inputFile = SOURCE_FOLDER + "invalidDocWithHugeRef.pdf";
NUnit.Framework.Assert.DoesNotThrow(() => new PdfDocument(new PdfReader(inputFile)));
}

[NUnit.Framework.Test]
[LogMessage(iText.IO.Logs.IoLogMessageConstant.XREF_ERROR_WHILE_READING_TABLE_WILL_BE_REBUILT, LogLevel =
LogLevelConstants.ERROR)]
public virtual void OpenWithWriterInvalidDocWithHugeRefTest() {
String inputFile = SOURCE_FOLDER + "invalidDocWithHugeRef.pdf";
MemoryStream outputStream = new ByteArrayOutputStream();
Exception e = NUnit.Framework.Assert.Catch(typeof(PdfException), () => new PdfDocument(new PdfReader(inputFile
), new PdfWriter(outputStream)));
NUnit.Framework.Assert.AreEqual(KernelExceptionMessageConstant.XREF_STRUCTURE_SIZE_EXCEEDED_THE_LIMIT, e.Message
);
}

[NUnit.Framework.Test]
public virtual void TestCreateAndUpdateXMP() {
String created = destinationFolder + "testCreateAndUpdateXMP_create.pdf";
String updated = destinationFolder + "testCreateAndUpdateXMP_update.pdf";
String created = DESTINATION_FOLDER + "testCreateAndUpdateXMP_create.pdf";
String updated = DESTINATION_FOLDER + "testCreateAndUpdateXMP_update.pdf";
PdfDocument pdfDocument = new PdfDocument(CompareTool.CreateTestPdfWriter(created));
pdfDocument.AddNewPage();
// create XMP metadata
Expand Down Expand Up @@ -80,9 +104,9 @@ 0000000561 00000 n

[NUnit.Framework.Test]
public virtual void TestCreateAndUpdateTwiceXMP() {
String created = destinationFolder + "testCreateAndUpdateTwiceXMP_create.pdf";
String updated = destinationFolder + "testCreateAndUpdateTwiceXMP_update.pdf";
String updatedAgain = destinationFolder + "testCreateAndUpdateTwiceXMP_updatedAgain.pdf";
String created = DESTINATION_FOLDER + "testCreateAndUpdateTwiceXMP_create.pdf";
String updated = DESTINATION_FOLDER + "testCreateAndUpdateTwiceXMP_update.pdf";
String updatedAgain = DESTINATION_FOLDER + "testCreateAndUpdateTwiceXMP_updatedAgain.pdf";
PdfDocument pdfDocument = new PdfDocument(CompareTool.CreateTestPdfWriter(created));
pdfDocument.AddNewPage();
// create XMP metadata
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public virtual void SetMaxNumberOfElementsInXrefStructure(int maxNumberOfElement
public virtual void CheckIfXrefStructureExceedsTheLimit(int requestedCapacity) {
// Objects in xref structures are using 1-based indexes, so to store maxNumberOfElementsInXrefStructure
// amount of elements we need maxNumberOfElementsInXrefStructure + 1 capacity.
if (requestedCapacity - 1 > maxNumberOfElementsInXrefStructure) {
if (requestedCapacity - 1 > maxNumberOfElementsInXrefStructure || requestedCapacity < 0) {
throw new MemoryLimitsAwareException(KernelExceptionMessageConstant.XREF_STRUCTURE_SIZE_EXCEEDED_THE_LIMIT
);
}
Expand Down
2 changes: 1 addition & 1 deletion itext/itext.kernel/itext/kernel/pdf/PdfXrefTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ internal virtual void InitFreeReferencesList(PdfDocument pdfDocument) {
// ensure zero object is free
xref[0].SetState(PdfObject.FREE);
SortedSet<int> freeReferences = new SortedSet<int>();
for (int i = 1; i < Size(); ++i) {
for (int i = 1; i < Size() && i < xref.Length; ++i) {
PdfIndirectReference @ref = xref[i];
if (@ref == null || @ref.IsFree()) {
freeReferences.Add(i);
Expand Down
2 changes: 1 addition & 1 deletion port-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3516ec7dcc9dd0e98b65eff9c9c8b7e49b218554
0ce21161eb41f1ab6e15ba5c31107d83f19c479c

0 comments on commit 8ff2cbe

Please sign in to comment.