Skip to content

Commit b607e83

Browse files
authored
Merge pull request #6510 from radarhere/buffer
2 parents 6697de1 + c463ef4 commit b607e83

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

Tests/images/mmap_error.bmp

9.04 KB
Binary file not shown.

Tests/test_file_bmp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def test_invalid_file():
3939
BmpImagePlugin.BmpImageFile(fp)
4040

4141

42+
def test_fallback_if_mmap_errors():
43+
# This image has been truncated,
44+
# so that the buffer is not large enough when using mmap
45+
with Image.open("Tests/images/mmap_error.bmp") as im:
46+
assert_image_equal_tofile(im, "Tests/images/pal8_offset.bmp")
47+
48+
4249
def test_save_to_bytes():
4350
output = io.BytesIO()
4451
im = hopper()

src/PIL/ImageFile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ def load(self):
192192

193193
with open(self.filename) as fp:
194194
self.map = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
195+
if offset + self.size[1] * args[1] > self.map.size():
196+
# buffer is not large enough
197+
raise OSError
195198
self.im = Image.core.map_buffer(
196199
self.map, self.size, decoder_name, offset, args
197200
)

0 commit comments

Comments
 (0)