Skip to content

Fix QoiDecoder crash on truncated QOI files#9813

Open
abhi-0203 wants to merge 1 commit into
python-pillow:mainfrom
abhi-0203:fix/qoi-truncated-index-error
Open

Fix QoiDecoder crash on truncated QOI files#9813
abhi-0203 wants to merge 1 commit into
python-pillow:mainfrom
abhi-0203:fix/qoi-truncated-index-error

Conversation

@abhi-0203

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

qoi truncated index error

2 participants