Don't use gigs of memory with invalid palette sizes

This commit is contained in:
wiredfool 2014-01-23 20:11:05 -08:00
parent 7d76ae2d91
commit 046a226f34
2 changed files with 27 additions and 0 deletions

View File

@ -129,6 +129,8 @@ class BmpImageFile(ImageFile.ImageFile):
greyscale = 1 greyscale = 1
if colors == 2: if colors == 2:
indices = (0, 255) indices = (0, 255)
elif colors > 2**16 or colors <=0: #We're reading a i32.
raise IOError("Unsupported BMP Palette size (%d)" % colors)
else: else:
indices = list(range(colors)) indices = list(range(colors))
for i in indices: for i in indices:

25
Tests/bmpsuite.py Normal file
View File

@ -0,0 +1,25 @@
from tester import *
from PIL import Image
import os
base = 'Tests/images/bmp/'
def get_files(d, ext='.bmp'):
return [os.path.join(base,d,f) for f
in os.listdir(os.path.join(base, d)) if ext in f]
def test_bad():
""" These shouldn't crash, but they shouldn't return anything either """
for f in get_files('b'):
try:
print ("Trying %s"%f)
im = Image.open(f)
print ("%s, %s" %(im.size, im.mode))
im.load()
except Exception as msg:
print ("Bad Image %s: %s" %(f,msg))