Skip to content

Comments

Fix BMP RLE delta escape reading from wrong file position#9443

Merged
hugovk merged 3 commits intopython-pillow:mainfrom
bysiber:fix-bmp-rle-delta-double-read
Feb 21, 2026
Merged

Fix BMP RLE delta escape reading from wrong file position#9443
hugovk merged 3 commits intopython-pillow:mainfrom
bysiber:fix-bmp-rle-delta-double-read

Conversation

@bysiber
Copy link
Contributor

@bysiber bysiber commented Feb 20, 2026

The BMP RLE decoder's delta escape handler reads the displacement bytes twice from the file stream. It reads 2 bytes into bytes_read for validation, then calls self.fd.read(2) again instead of unpacking bytes_read:

bytes_read = self.fd.read(2)   # read delta bytes
if len(bytes_read) < 2:
    break
right, up = self.fd.read(2)    # reads 2 MORE bytes, discards the actual delta values

The real delta displacement values are thrown away, and right / up come from whatever happens to be next in the stream. Any RLE-compressed BMP that uses delta escape codes gets decoded with wrong pixel offsets, producing corrupted output.

@radarhere
Copy link
Member

https://www.digicamsoft.com/bmp/bmp.html supports your change

2 Delta. The two bytes following the escape contain unsigned values indicating the horizontal and vertical offsets of the next pixel from the current position.

@bysiber
Copy link
Contributor Author

bysiber commented Feb 20, 2026

Thanks for confirming with the spec, glad the fix aligns with what the BMP reference says.

@radarhere radarhere mentioned this pull request Feb 20, 2026
@radarhere
Copy link
Member

I've created bysiber#3 to add a test.

@hugovk hugovk merged commit 4777a0b into python-pillow:main Feb 21, 2026
56 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.

3 participants