From 4b43e717c1422df5f6684c91d2aa546c7b70f76c Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 24 Jan 2026 11:08:38 +1100 Subject: [PATCH] Do not attempt to read zero bytes --- src/PIL/DdsImagePlugin.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/PIL/DdsImagePlugin.py b/src/PIL/DdsImagePlugin.py index 063df1974..897c0741f 100644 --- a/src/PIL/DdsImagePlugin.py +++ b/src/PIL/DdsImagePlugin.py @@ -489,9 +489,14 @@ class DdsRgbDecoder(ImageFile.PyDecoder): _pulls_fd = True def decode(self, buffer: bytes | Image.SupportsArrayInterface) -> tuple[int, int]: - assert self.fd is not None bitcount, masks = self.args + data = bytearray() + bytecount = bitcount // 8 + if not bytecount: + self.set_as_raw(data) + return -1, 0 + # Some masks will be padded with zeros, e.g. R 0b11 G 0b1100 # Calculate how many zeros each mask is padded with mask_offsets = [] @@ -505,8 +510,7 @@ class DdsRgbDecoder(ImageFile.PyDecoder): mask_offsets.append(offset) mask_totals.append(mask >> offset) - data = bytearray() - bytecount = bitcount // 8 + assert self.fd is not None dest_length = self.state.xsize * self.state.ysize * len(masks) while len(data) < dest_length: bytes_read = self.fd.read(bytecount)