Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent division by zero #8408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

radarhere
Copy link
Member

Resolves #8405. Alternative to #8406

The issue is concerned that at

if (INT32_MAX / state->xsize < state->ysize) {

state->xsize might be zero, and so we might be dividing by zero.

However, in the context of our library as a whole, images that say one of their dimensions are zero will be stopped at

Pillow/src/PIL/ImageFile.py

Lines 154 to 156 in 731bcda

if not self.mode or self.size[0] <= 0 or self.size[1] <= 0:
msg = "not identified by this driver"
raise SyntaxError(msg)

Even if you consider just the C decoding process, we have

Pillow/src/decode.c

Lines 189 to 192 in 731bcda

if (state->xsize <= 0 || state->xsize + state->xoff > (int)im->xsize ||
state->ysize <= 0 || state->ysize + state->yoff > (int)im->ysize) {
PyErr_SetString(PyExc_ValueError, "tile cannot extend outside image");
return NULL;

So this is not a scenario that should actually occur. However, in order to allay concerns from a casual observer, it might be worth updating the code. I don't consider changes to address this to be a 'fix', but rather a 'If all else fails' safety net.

#8406 suggests resolving the concern by raising an error from within FliDecode.c. My minor concern with that strategy is that we could make someone reading the code think that xsize might be zero there.

Instead, I'm going to suggest just removing the division operation altogether.

if (INT32_MAX < (long)state->xsize * state->ysize) {

@Yay295
Copy link
Contributor

Yay295 commented Sep 23, 2024

long is not necessarily 64 bits. https://learn.microsoft.com/en-us/cpp/build/common-visual-cpp-64-bit-migration-issues
You should use int64_t.

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.

Unused code in FliDecode.c and _imaging.c has SAST security issue
2 participants