Merge pull request #4283 from radarhere/bmp

Raise an error if BMP file size is too large when saving
This commit is contained in:
Hugo van Kemenade 2019-12-21 11:05:03 +02:00 committed by GitHub
commit 33cfac7715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -41,6 +41,13 @@ class TestFileBmp(PillowTestCase):
self.assertEqual(im.size, reloaded.size)
self.assertEqual(reloaded.format, "BMP")
def test_save_too_large(self):
outfile = self.tempfile("temp.bmp")
with Image.new("RGB", (1, 1)) as im:
im._size = (37838, 37838)
with self.assertRaises(ValueError):
im.save(outfile)
def test_dpi(self):
dpi = (72, 72)

View File

@ -321,12 +321,15 @@ def _save(im, fp, filename, bitmap_header=True):
# bitmap header
if bitmap_header:
offset = 14 + header + colors * 4
file_size = offset + image
if file_size > 2 ** 32 - 1:
raise ValueError("File size is too large for the BMP format")
fp.write(
b"BM"
+ o32(offset + image) # file type (magic)
+ o32(0) # file size
+ o32(offset) # reserved
) # image data offset
b"BM" # file type (magic)
+ o32(file_size) # file size
+ o32(0) # reserved
+ o32(offset) # image data offset
)
# bitmap info header
fp.write(