Skip to content

Commit

Permalink
Merge pull request #6510 from radarhere/buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Aug 31, 2022
2 parents 6697de1 + c463ef4 commit b607e83
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
Binary file added Tests/images/mmap_error.bmp
Binary file not shown.
7 changes: 7 additions & 0 deletions Tests/test_file_bmp.py
Expand Up @@ -39,6 +39,13 @@ def test_invalid_file():
BmpImagePlugin.BmpImageFile(fp)


def test_fallback_if_mmap_errors():
# This image has been truncated,
# so that the buffer is not large enough when using mmap
with Image.open("Tests/images/mmap_error.bmp") as im:
assert_image_equal_tofile(im, "Tests/images/pal8_offset.bmp")


def test_save_to_bytes():
output = io.BytesIO()
im = hopper()
Expand Down
3 changes: 3 additions & 0 deletions src/PIL/ImageFile.py
Expand Up @@ -192,6 +192,9 @@ def load(self):

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

0 comments on commit b607e83

Please sign in to comment.