Fix ZeroDivisionError in DdsImagePlugin (#9272)

This commit is contained in:
Hugo van Kemenade 2025-10-24 16:44:46 +03:00 committed by GitHub
commit 148a19eee4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 0 deletions

Binary file not shown.

View File

@ -380,6 +380,11 @@ def test_palette() -> None:
assert_image_equal_tofile(im, "Tests/images/transparent.gif")
def test_zero_mask_totals() -> None:
with Image.open("Tests/images/zero_mask_totals.dds") as im:
im.load()
def test_unsupported_header_size() -> None:
with pytest.raises(OSError, match="Unsupported header size 0"):
with Image.open(BytesIO(b"DDS " + b"\x00" * 4)):

View File

@ -333,6 +333,7 @@ class DdsImageFile(ImageFile.ImageFile):
format_description = "DirectDraw Surface"
def _open(self) -> None:
assert self.fp is not None
if not _accept(self.fp.read(4)):
msg = "not a DDS file"
raise SyntaxError(msg)
@ -516,6 +517,8 @@ class DdsRgbDecoder(ImageFile.PyDecoder):
# Remove the zero padding, and scale it to 8 bits
data += o8(
int(((masked_value >> mask_offsets[i]) / mask_totals[i]) * 255)
if mask_totals[i]
else 0
)
self.set_as_raw(data)
return -1, 0