Skip to content

OutOfMemoryException in Parser.ReadTrailer() when opening a valid ~1 GiB PDF #390

Description

@359193585

OutOfMemoryException when opening a valid ~1 GiB PDF in PdfReader.Open()

Description

I encountered an OutOfMemoryException when opening a valid PDF file of approximately 1 GiB with PDFsharp 6.2.4.

The PDF can be opened normally by common PDF readers.

Interestingly, I also tested PDFsharp with a real multi-page PDF of approximately 13 GiB, and PDFsharp was able to open and merge that file successfully (although it was understandably slow). Therefore, this does not appear to be a simple file-size limitation.

The issue seems to be related to a particular PDF structure and occurs while PDFsharp is parsing the trailer.

Environment

  • PDFsharp: 6.2.4
  • .NET: 8.0
  • OS: Windows 11
  • x64

Reproduction

The exception occurs with:

using var document =
    PdfReader.Open(filePath, PdfDocumentOpenMode.Import);

The file is approximately 1 GiB in size.

Exception

System.OutOfMemoryException
  HResult=0x8007000E
  Message=Exception of type 'System.OutOfMemoryException' was thrown.

Stack trace:

System.String.Ctor(Char[] value)
System.Text.Encoding.GetString(Byte[] bytes, Int32 index, Int32 count)
PdfSharp.Pdf.IO.Lexer.ScanRawString(Int64 position, Int32 length)
PdfSharp.Pdf.IO.Parser.ReadTrailer()
PdfSharp.Pdf.IO.PdfReader.OpenFromStream(...)
PdfSharp.Pdf.IO.PdfReader.OpenFromFile(...)
PdfSharp.Pdf.IO.PdfReader.Open(...)
PdfFormatDetector.Detect(...)

The relevant part appears to be:

Parser.ReadTrailer()
    -> Lexer.ScanRawString(...)
        -> Encoding.GetString(...)
            -> OutOfMemoryException

This suggests that ScanRawString() is attempting to materialize a very large section of the PDF as a .NET string.

Additional tests

I performed several tests to determine whether this was simply caused by the file size.

1. The original ~1 GiB PDF

  • Opens successfully in common PDF readers.
  • PdfReader.Open(..., Import) in PDFsharp 6.2.4 throws OutOfMemoryException.

2. Re-saving the PDF

I opened the PDF in another PDF application and used "Save As" to save it again.

The resulting PDF was still around 1 GiB and PDFsharp still threw the same OutOfMemoryException.

3. Printing the PDF to another PDF

I printed the document using a PDF printer, which regenerated the PDF.

The resulting file was only approximately 536 KB.

PDFsharp could open the regenerated PDF successfully.

4. A real ~13 GiB PDF

I also tested a real multi-page PDF of approximately 13 GiB.

PDFsharp was able to open it and successfully merge it. It was slow, but it did not run out of memory.

This makes me think the problem is related to a particular PDF structure rather than the absolute file size.

Expected behavior

PdfReader.Open() should either:

  1. successfully open the PDF without allocating an unreasonable amount of memory, or
  2. fail with a meaningful PDF parsing/compatibility exception if the PDF contains a structure that PDFsharp cannot process.

It would be preferable to avoid attempting to create a huge .NET string from an unexpectedly large range of the input file.

Question

Could this be related to how Parser.ReadTrailer() determines the range passed to Lexer.ScanRawString()?

The stack trace suggests that a very large range is eventually passed to:

Encoding.GetString(byte[], int, int)

which causes the large string allocation and the OutOfMemoryException.

I can provide the problematic PDF if it would help with reproducing and diagnosing the issue.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions