Skip to content

ROB: Do not crash when the font widths are not an array - #4118

Merged
stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:widths-not-an-array
Sep 22, 2026
Merged

stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:widths-not-an-array

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

_collect_tt_t1_character_widths casts the /Widths entry of a TrueType or Type1
font to an ArrayObject and enumerates it:

widths_array = cast(ArrayObject, pdf_font_dict["/Widths"])
for idx, width in enumerate(widths_array):
    current_widths[chr(idx + first_char)] = int(width)

The cast has no effect at runtime, so a file storing a string there is iterated
character by character and raises 'ValueError: invalid literal for int() with
base 10' out of extract_text().

The entry is now checked and a font whose widths cannot be read is reported and
skipped, which is how the loop below already handles a non-numeric width entry.

Offline suite passes, 1390 tests.

Used Claude (Opus) to help locate and patch this; I reviewed and tested the change.

_collect_tt_t1_character_widths casts the /Widths entry of a TrueType or Type1
font to an ArrayObject and enumerates it, so a file storing a string there
raised 'ValueError: invalid literal for int() with base 10' out of
extract_text() when the characters were passed to int().

The entry is now checked and a font whose widths cannot be read is reported and
skipped, which is how the surrounding code already handles a non-numeric width
entry.
@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.10%. Comparing base (359f70d) to head (713bfa4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4118   +/-   ##
=======================================
  Coverage   98.10%   98.10%           
=======================================
  Files          59       59           
  Lines       11559    11562    +3     
  Branches     2161     2162    +1     
=======================================
+ Hits        11340    11343    +3     
  Misses        124      124           
  Partials       95       95           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stefan6419846
stefan6419846 merged commit ebae1a5 into py-pdf:main Sep 22, 2026
26 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants