Skip to content

Raise ValueError if P;2L or P;4L data is truncated in frombytes()#9725

Merged
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:frombytes_multiple
Jun 27, 2026
Merged

Raise ValueError if P;2L or P;4L data is truncated in frombytes()#9725
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:frombytes_multiple

Conversation

@radarhere

@radarhere radarhere commented Jun 27, 2026

Copy link
Copy Markdown
Member

When passing data to frombytes() (or frombuffer() or im.frombytes()), it is possible to pass an insufficient amount of data. These unpackers are unusual in that they require data in multiples of 4 or 8 bytes respectively.

This is described at https://web.archive.org/web/20100206055706/http://www.qzx.com/pc-gpe/pcx.txt, where NPlanes is the 2 or 4 in P;2L or P;4L

Then calculate how many bytes are required to hold one complete uncompressed scan line:

TotalBytes = NPlanes * BytesPerLine

Note that since there are always an even number of bytes per scan line,

This PR raises a ValueError if the data is truncated.

@radarhere radarhere changed the title Raise error in frombytes() if P;2L or P;4L data is truncated Raise ValueError if P;2L or P;4L data is truncated in frombytes() Jun 27, 2026
@radarhere radarhere merged commit 095bbc3 into python-pillow:main Jun 27, 2026
51 of 52 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.

1 participant