dedup.image: img.convert can also raise that crazy stuff
[~helmut/debian-dedup.git] / dedup / image.py
index e05e7da..1148890 100644 (file)
@@ -50,7 +50,10 @@ class ImageHash(object):
         pos = self.content.tell()
         try:
             self.content.seek(0)
-            img = PIL.Image.open(self.content)
+            try:
+                img = PIL.Image.open(self.content)
+            except IOError:
+                raise ValueError("broken png header")
             width, height = img.size
             pack = lambda elem: struct.pack("BBBB", *elem)
             # special casing easy modes reduces memory usage
@@ -59,9 +62,15 @@ class ImageHash(object):
             elif img.mode == "RGB":
                 pack = lambda elem: struct.pack("BBBB", *(elem + (255,)))
             elif img.mode != "RGBA":
-                img = img.convert("RGBA")
-            for elem in img.getdata():
-                hashobj.update(pack(elem))
+                try:
+                    img = img.convert("RGBA")
+                except (SyntaxError, IndexError, IOError): # crazy stuff from PIL
+                    raise ValueError("error reading png image")
+            try:
+                for elem in img.getdata():
+                    hashobj.update(pack(elem))
+            except (SyntaxError, IndexError, IOError): # crazy stuff from PIL
+                raise ValueError("error reading png image")
         finally:
             self.content.seek(pos)
         return "%s%8.8x%8.8x" % (hashobj.hexdigest(), width, height)