Skip to content

Prevent integer overflow when reducing I images - #10514

Open
patrick-immometrica wants to merge 2 commits into
python-pillow:mainfrom
patrick-immometrica:reduce-int32-overflow
Open

patrick-immometrica wants to merge 2 commits into
python-pillow:mainfrom
patrick-immometrica:reduce-int32-overflow

Conversation

@patrick-immometrica

Copy link
Copy Markdown

Changes proposed in this pull request:

  • reduce() on I images adds up to four INT32 pixels as int before the result goes into the double sum, so it overflows once values are above about 2**29.
from PIL import Image

im = Image.new("I", (4, 4), 2**31 - 1)
print(im.reduce(2).getpixel((0, 0)))  # -1
print(im.resize((2, 2), Image.Resampling.BOX).getpixel((0, 0)))  # 2147483647
  • Cast the first term to double so the whole expression is evaluated as double.

This branch has not been deployed

No deployments
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