Skip to content

Commit 801f7ad

Browse files
authored
Simplify check for GBR width and height (#9089)
2 parents bb8dfa4 + 4adff39 commit 801f7ad

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

Tests/test_file_gbr.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import annotations
22

3+
from io import BytesIO
4+
35
import pytest
46

5-
from PIL import GbrImagePlugin, Image
7+
from PIL import GbrImagePlugin, Image, _binary
68

79
from .helper import assert_image_equal_tofile
810

@@ -31,8 +33,49 @@ def test_multiple_load_operations() -> None:
3133
assert_image_equal_tofile(im, "Tests/images/gbr.png")
3234

3335

36+
def create_gbr_image(info: dict[str, int] = {}, magic_number=b"") -> BytesIO:
37+
return BytesIO(
38+
b"".join(
39+
_binary.o32be(i)
40+
for i in [
41+
info.get("header_size", 20),
42+
info.get("version", 1),
43+
info.get("width", 1),
44+
info.get("height", 1),
45+
info.get("color_depth", 1),
46+
]
47+
)
48+
+ magic_number
49+
)
50+
51+
3452
def test_invalid_file() -> None:
35-
invalid_file = "Tests/images/flower.jpg"
53+
for f in [
54+
create_gbr_image({"header_size": 0}),
55+
create_gbr_image({"width": 0}),
56+
create_gbr_image({"height": 0}),
57+
]:
58+
with pytest.raises(SyntaxError, match="not a GIMP brush"):
59+
GbrImagePlugin.GbrImageFile(f)
3660

37-
with pytest.raises(SyntaxError):
61+
invalid_file = "Tests/images/flower.jpg"
62+
with pytest.raises(SyntaxError, match="Unsupported GIMP brush version"):
3863
GbrImagePlugin.GbrImageFile(invalid_file)
64+
65+
66+
def test_unsupported_gimp_brush() -> None:
67+
f = create_gbr_image({"color_depth": 2})
68+
with pytest.raises(SyntaxError, match="Unsupported GIMP brush color depth: 2"):
69+
GbrImagePlugin.GbrImageFile(f)
70+
71+
72+
def test_bad_magic_number() -> None:
73+
f = create_gbr_image({"version": 2}, magic_number=b"badm")
74+
with pytest.raises(SyntaxError, match="not a GIMP brush, bad magic number"):
75+
GbrImagePlugin.GbrImageFile(f)
76+
77+
78+
def test_L() -> None:
79+
f = create_gbr_image()
80+
with Image.open(f) as im:
81+
assert im.mode == "L"

src/PIL/GbrImagePlugin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _open(self) -> None:
5454
width = i32(self.fp.read(4))
5555
height = i32(self.fp.read(4))
5656
color_depth = i32(self.fp.read(4))
57-
if width <= 0 or height <= 0:
57+
if width == 0 or height == 0:
5858
msg = "not a GIMP brush"
5959
raise SyntaxError(msg)
6060
if color_depth not in (1, 4):
@@ -71,7 +71,7 @@ def _open(self) -> None:
7171
raise SyntaxError(msg)
7272
self.info["spacing"] = i32(self.fp.read(4))
7373

74-
comment = self.fp.read(comment_length)[:-1]
74+
self.info["comment"] = self.fp.read(comment_length)[:-1]
7575

7676
if color_depth == 1:
7777
self._mode = "L"
@@ -80,8 +80,6 @@ def _open(self) -> None:
8080

8181
self._size = width, height
8282

83-
self.info["comment"] = comment
84-
8583
# Image might not be small
8684
Image._decompression_bomb_check(self.size)
8785

0 commit comments

Comments
 (0)