Skip to content

Commit

Permalink
Use _accept check in _open
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 26, 2022
1 parent cda5139 commit c65c1e1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Tests/test_file_dds.py
Expand Up @@ -196,6 +196,13 @@ def test__accept_false():
assert not output


def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"

with pytest.raises(SyntaxError):
DdsImagePlugin.DdsImageFile(invalid_file)


def test_short_header():
"""Check a short header"""
with open(TEST_FILE_DXT5, "rb") as f:
Expand Down
7 changes: 7 additions & 0 deletions Tests/test_file_ftex.py
Expand Up @@ -16,6 +16,13 @@ def test_load_dxt1():
assert_image_similar(im, target.convert("RGBA"), 15)


def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"

with pytest.raises(SyntaxError):
FtexImagePlugin.FtexImageFile(invalid_file)


def test_constants_deprecation():
for enum, prefix in {
FtexImagePlugin.Format: "FORMAT_",
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/DdsImagePlugin.py
Expand Up @@ -111,7 +111,9 @@ class DdsImageFile(ImageFile.ImageFile):
format_description = "DirectDraw Surface"

def _open(self):
magic, header_size = struct.unpack("<II", self.fp.read(8))
if not _accept(self.fp.read(4)):
raise SyntaxError("not a DDS file")
(header_size,) = struct.unpack("<I", self.fp.read(4))
if header_size != 124:
raise OSError(f"Unsupported header size {repr(header_size)}")
header_bytes = self.fp.read(header_size - 4)
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/FtexImagePlugin.py
Expand Up @@ -94,7 +94,8 @@ class FtexImageFile(ImageFile.ImageFile):
format_description = "Texture File Format (IW2:EOC)"

def _open(self):
struct.unpack("<I", self.fp.read(4)) # magic
if not _accept(self.fp.read(4)):
raise SyntaxError("not a FTEX file")
struct.unpack("<i", self.fp.read(4)) # version
self._size = struct.unpack("<2i", self.fp.read(8))
mipmap_count, format_count = struct.unpack("<2i", self.fp.read(8))
Expand Down

0 comments on commit c65c1e1

Please sign in to comment.