mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-11 04:07:21 +03:00
Merge pull request #4283 from radarhere/bmp
Raise an error if BMP file size is too large when saving
This commit is contained in:
commit
33cfac7715
|
@ -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)
|
||||
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue
Block a user