Skip to content

Prevent opening P TGA images without a palette #7797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Raise an error if map depth is unknown
  • Loading branch information
radarhere committed Feb 13, 2024
commit 818500b329555969cdb852c81d667cc70faaed94
Binary file added Tests/images/p_8.tga
Binary file not shown.
7 changes: 6 additions & 1 deletion Tests/test_file_tga.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from PIL import Image
from PIL import Image, UnidentifiedImageError

from .helper import assert_image_equal, assert_image_equal_tofile, hopper

Expand Down Expand Up @@ -65,6 +65,11 @@ def roundtrip(original_im) -> None:
roundtrip(original_im)


def test_palette_depth_8(tmp_path: Path) -> None:
with pytest.raises(UnidentifiedImageError):
Image.open("Tests/images/p_8.tga")


def test_palette_depth_16(tmp_path: Path) -> None:
with Image.open("Tests/images/p_16.tga") as im:
assert_image_equal_tofile(im.convert("RGB"), "Tests/images/p_16.png")
Expand Down
3 changes: 3 additions & 0 deletions src/PIL/TgaImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ def _open(self) -> None:
self.palette = ImagePalette.raw(
"BGRA", b"\0" * 4 * start + self.fp.read(4 * size)
)
else:
msg = "unknown TGA map depth"
raise SyntaxError(msg)

# setup tile descriptor
try:
Expand Down