diff -rupN --no-dereference Pillow-5.4.1/src/libImaging/PcxDecode.c Pillow-5.4.1-new/src/libImaging/PcxDecode.c --- Pillow-5.4.1/src/libImaging/PcxDecode.c 2020-02-13 14:44:21.491015777 +0100 +++ Pillow-5.4.1-new/src/libImaging/PcxDecode.c 2020-02-13 14:44:21.593015020 +0100 @@ -25,6 +25,9 @@ ImagingPcxDecode(Imaging im, ImagingCode if (strcmp(im->mode, "1") == 0 && state->xsize > state->bytes * 8) { state->errcode = IMAGING_CODEC_OVERRUN; return -1; + } else if (strcmp(im->mode, "P") == 0 && state->xsize > state->bytes) { + state->errcode = IMAGING_CODEC_OVERRUN; + return -1; } ptr = buf; diff -rupN --no-dereference Pillow-5.4.1/Tests/test_image.py Pillow-5.4.1-new/Tests/test_image.py --- Pillow-5.4.1/Tests/test_image.py 2020-02-13 14:44:21.592015027 +0100 +++ Pillow-5.4.1-new/Tests/test_image.py 2020-02-13 14:44:21.594015013 +0100 @@ -555,6 +555,17 @@ class TestRegistry(PillowTestCase): self.assertIsInstance(enc, MockEncoder) self.assertEqual(enc.args, ('RGB', 'args', 'extra')) + def test_overrun(self): + for file in [ + "pcx_overrun2.bin", + ]: + im = Image.open(os.path.join("Tests/images", file)) + try: + im.load() + self.assertFail() + except IOError as e: + self.assertEqual(str(e), "buffer overrun when reading image file") + def test_encode_registry_fail(self): self.assertRaises(IOError, Image._getencoder, 'RGB', 'DoesNotExist',