diff --git a/PIL/BmpImagePlugin.py b/PIL/BmpImagePlugin.py index a4eb1d3f2..87e5f7888 100644 --- a/PIL/BmpImagePlugin.py +++ b/PIL/BmpImagePlugin.py @@ -129,6 +129,8 @@ class BmpImageFile(ImageFile.ImageFile): greyscale = 1 if colors == 2: indices = (0, 255) + elif colors > 2**16 or colors <=0: #We're reading a i32. + raise IOError("Unsupported BMP Palette size (%d)" % colors) else: indices = list(range(colors)) for i in indices: diff --git a/Tests/bmpsuite.py b/Tests/bmpsuite.py new file mode 100644 index 000000000..7878da2b4 --- /dev/null +++ b/Tests/bmpsuite.py @@ -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)) + + +