Skip to content

Raise ValueError instead of asserting number of FTEX texture formats#9805

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
ritsth:fix/ftex-assert-format-count
Jul 22, 2026
Merged

Raise ValueError instead of asserting number of FTEX texture formats#9805
radarhere merged 2 commits into
python-pillow:mainfrom
ritsth:fix/ftex-assert-format-count

Conversation

@ritsth

@ritsth ritsth commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Resolves #9809

FtexImageFile._open used assert format_count == 1 to reject multi-format files. That raises AssertionError, which is not one of the exceptions Pillow documents for unparseable files, so callers catching UnidentifiedImageError or ValueError do not catch it; and under python -O the assert is stripped, so the file is parsed as if valid.

This raises a ValueError instead, matching the existing "Invalid texture compression format" check in the same plugin. Added a regression test that patches the format count in a valid file, mirroring test_invalid_texture.

Used an AI assistant to find this; I reviewed and tested the change myself.

@radarhere radarhere added the 🤖-assisted AI-assisted label Jul 21, 2026
@radarhere

Copy link
Copy Markdown
Member

So are you saying you actually encountered a multi-format file in the wild?

@radarhere

Copy link
Copy Markdown
Member

I've created ritsth#1 with a suggestion.

@ritsth

ritsth commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

So are you saying you actually encountered a multi-format file in the wild?

No, I haven't encountered one in the wild. I found this while reading through the FTEX plugin's _open() and noticed the assert format_count == 1 was being used for input validation rather than an internal invariant check. That's a problem regardless of whether a real multi-format file exists, since assert raises an undocumented AssertionError and disappears entirely under python -O, so malformed/truncated input could either crash confusingly or silently skip validation.

I've created ritsth#1 with a suggestion.

Also applied your wording suggestion from ritsth#1. Thanks!

@radarhere radarhere changed the title Raise ValueError instead of asserting on FTEX format count Raise ValueError instead of asserting FTEX format count Jul 22, 2026
@radarhere radarhere changed the title Raise ValueError instead of asserting FTEX format count Raise ValueError instead of asserting number of FTEX texture formats Jul 22, 2026
@radarhere
radarhere merged commit 9e282f5 into python-pillow:main Jul 22, 2026
65 of 67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reachable assert in FtexImagePlugin.py

2 participants