Skip to content

Fix unexpected errors when decoding truncated QOI images - #9813

Merged
hugovk merged 2 commits into
python-pillow:mainfrom
abhi-0203:fix/qoi-truncated-index-error
Jul 27, 2026
Merged

Fix unexpected errors when decoding truncated QOI images#9813
hugovk merged 2 commits into
python-pillow:mainfrom
abhi-0203:fix/qoi-truncated-index-error

Conversation

@abhi-0203

Copy link
Copy Markdown
Contributor

Summary

Fixes a crash in QoiDecoder.decode() when a truncated QOI file is opened. The decoder now raises OSError with the standard "image file is truncated" message instead of an uncaught IndexError.

Problem

When a truncated QOI file is opened, QoiDecoder.decode() reads past EOF and indexes the empty result of read(1), causing an IndexError:

byte = self.fd.read(1)[0]  # IndexError: index out of range when read returns b''

This affects multiple read sites in the decoder:

  • The initial opcode byte read (line 64)
  • The QOI_OP_RGB 3-byte payload read (line 67)
  • The QOI_OP_RGBA 4-byte payload read (line 69)
  • The QOI_OP_LUMA second byte read (line 89)

Fix

Added length checks before indexing all read() results. When a short read is detected (EOF), the decoder raises OSError("image file is truncated"), consistent with other Pillow decoders (JPEG, PNG, etc.).

Testing

The fix can be verified by opening a truncated QOI file:

from PIL import Image
im = Image.open("truncated.qoi")
im.load()  # Previously: IndexError, Now: OSError("image file is truncated")

Fixes #9812

Check for empty reads before indexing in QoiDecoder.decode() to prevent
IndexError when a truncated QOI file is opened. The decoder now raises
OSError with 'image file is truncated' message, consistent with other
Pillow decoders.

Fixes python-pillow#9812
@radarhere

Copy link
Copy Markdown
Member

I've created abhi-0203#1 with a suggestion.

@radarhere radarhere changed the title Fix QoiDecoder crash on truncated QOI files Fix unexpected errors when decoding truncated QOI images Jul 27, 2026
@hugovk
hugovk merged commit 703e5d1 into python-pillow:main Jul 27, 2026
50 of 51 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.

qoi truncated index error

3 participants