BUG: resolve UnboundLocalError for xobjs in _get_image#3684
BUG: resolve UnboundLocalError for xobjs in _get_image#3684stefan6419846 merged 6 commits intopy-pdf:mainfrom
Conversation
The xobjs variable was used outside the try-except block that defined it. If a KeyError was caught and the id started/ended with '~' (inline images), the code would continue but xobjs would remain undefined, causing UnboundLocalError when trying to access non-inline images. Initialize xobjs to None and check before using it.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3684 +/- ##
==========================================
+ Coverage 97.41% 97.42% +0.01%
==========================================
Files 55 55
Lines 9989 9990 +1
Branches 1833 1835 +2
==========================================
+ Hits 9731 9733 +2
+ Misses 150 149 -1
Partials 108 108 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
xobjs in _get_image method
stefan6419846
left a comment
There was a problem hiding this comment.
Thanks for the report. Please revert all unrelated changes and add a corresponding test to allow me to continue with the review.
|
Reverted the unrelated formatting changes and kept the _get_image fix minimal. I also added a regression test in ests/test_images.py covering inline image access when /XObject resources are missing. |
|
All requested changes should now be addressed: the unrelated formatting changes were reverted, a regression test was added, and the full CI is now green. If you have time, I'd appreciate another look. |
|
Addressed the latest review notes: I removed the new |
|
Could you please add tests for the error cases as well? |
|
Added the requested error-case coverage as well:
I also tightened the |
## What's new ### Security (SEC) - Avoid infinite loop in read_from_stream for broken files (#3693) by @stefan6419846 ### Robustness (ROB) - Resolve UnboundLocalError for xobjs in _get_image (#3684) by @Yuki9814 [Full Changelog](6.9.1...6.9.2)
Description
Fixes a potential
UnboundLocalErrorin the_get_image()method ofpypdf/_page.py.The Bug
In
pypdf/_page.py, the_get_image()method uses atry-exceptblock to retrieve XObject resources:If a
KeyErroris raised (no XObject resources) but theidstarts and ends with~(indicating an inline image), the exception is silently caught and the code continues. However, for non-inline images, the subsequent code tries to accessxobjs, which was never assigned, causingUnboundLocalError: local variable 'xobjs' referenced before assignment.The Fix
Initialize
xobjs = Nonebefore thetryblock and add explicit checks before using it:This ensures that if
xobjsisNone, we raise a clearKeyErrorinstead of crashing with anUnboundLocalError.Testing
The fix maintains backward compatibility:
~0~) continue to work without XObject resourcesKeyErrorif XObject resources are missing