Maruan Sahyoun created PDFBOX-6369:
--------------------------------------

             Summary: Avoid excessive memory use when reading GSUB tables
                 Key: PDFBOX-6369
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6369
             Project: PDFBox
          Issue Type: Bug
            Reporter: Maruan Sahyoun


Reading the GSUB table of a TrueType or OpenType font can take far more memory 
than the size of the table. A format 2 coverage table is expanded into one 
boxed `Integer` per glyph, so that each 6-byte range record can result in up to 
65536 objects. Subtables referenced several times through their offsets are 
read again for each reference. In addition, the `GsubData` used for writing 
text is built eagerly when the font is parsed, with a complete copy of the 
substitutions for each feature tag, although fonts loaded from a PDF never use 
it. A GSUB table of less than 1 KB with 100 feature tags sharing one lookup 
retains about 850 MB, and a table of a few KB causes an `OutOfMemoryError` 
while a document with such an embedded font is being opened.

Found by a Claude security scan.

Proposed change
 - Build the `GsubData` on the first call of `getGsubData()` instead of when 
the table is read.
 - Limit the substitution entries in the `GsubData` to 65536 or 8 times the 
table size, whichever is larger; if the limit is reached, skip the remaining 
entries and log a warning.
 - Keep the range records of format 2 coverage tables and look up glyphs by 
binary search instead of expanding them.
 - Limit the entries created while reading a GSUB table in proportion to the 
table size; if the limit is exceeded, log a warning and treat the table as 
empty.
 - Add regression tests for coverage tables with many ranges, for many feature 
tags and for shared subtables.

Claude on behalf of Maruan Sahyoun (msahyoun)



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to