# Patch-overlapping Python context after the fix ## `Tests/test_image.py` — `class:TestImage` (lines 21-818) ```python class TestImage: def test_image_modes_success(self): for mode in [ "1", "P", "PA", "L", "LA", "La", "F", "I", "I;16", "I;16L", "I;16B", "I;16N", "RGB", "RGBX", "RGBA", "RGBa", "CMYK", "YCbCr", "LAB", "HSV", ]: Image.new(mode, (1, 1)) def test_image_modes_fail(self): for mode in [ "", "bad", "very very long", "BGR;15", "BGR;16", "BGR;24", "BGR;32", ]: with pytest.raises(ValueError) as e: Image.new(mode, (1, 1)) assert str(e.value) == "unrecognized image mode" def test_exception_inheritance(self): assert issubclass(UnidentifiedImageError, OSError) def test_sanity(self): im = Image.new("L", (100, 100)) assert repr(im)[:45] == "= 7 with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION < "9.9.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION <= "9.9.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION != "7.0.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION >= "7.0.0" with pytest.warns(DeprecationWarning): assert test_module.PILLOW_VERSION > "7.0.0" @pytest.mark.parametrize("path", [ "fli_overrun.bin", "sgi_overrun.bin", "sgi_overrun_expandrow.bin", "sgi_overrun_expandrow2.bin", "pcx_overrun.bin", "pcx_overrun2.bin", "ossfuzz-4836216264589312.pcx", "01r_00.pcx", ]) def test_overrun(self, path): """For overrun completeness, test as: valgrind pytest -qq Tests/test_image.py::TestImage::test_overrun | grep decode.c """ with Image.open(os.path.join("Tests/images", path)) as im: try: im.load() assert False except OSError as e: assert (str(e) == "buffer overrun when reading image file" or "image file is truncated" in str(e)) def test_fli_overrun2(self): with Image.open("Tests/images/fli_overrun2.bin") as im: try: im.seek(1) assert False except OSError as e: assert str(e) == "buffer overrun when reading image file" def test_show_deprecation(self, monkeypatch): monkeypatch.setattr(Image, "_show", lambda *args, **kwargs: None) im = Image.new("RGB", (50, 50), "white") with pytest.warns(None) as raised: im.show() assert not raised with pytest.warns(DeprecationWarning): im.show(command="mock") ``` ## `Tests/test_image.py` — `function:TestImage.test_overrun` (lines 778-798) ```python def test_overrun(self, path): """For overrun completeness, test as: valgrind pytest -qq Tests/test_image.py::TestImage::test_overrun | grep decode.c """ with Image.open(os.path.join("Tests/images", path)) as im: try: im.load() assert False except OSError as e: assert (str(e) == "buffer overrun when reading image file" or "image file is truncated" in str(e)) ``` ## `Tests/test_image.py` — `function:TestImage.test_fli_overrun2` (lines 800-806) ```python def test_fli_overrun2(self): with Image.open("Tests/images/fli_overrun2.bin") as im: try: im.seek(1) assert False except OSError as e: assert str(e) == "buffer overrun when reading image file" ``` ## `src/PIL/PcxImagePlugin.py` — `class:PcxImageFile` (lines 47-121) ```python class PcxImageFile(ImageFile.ImageFile): format = "PCX" format_description = "Paintbrush" def _open(self): # header s = self.fp.read(128) if not _accept(s): raise SyntaxError("not a PCX file") # image bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1 if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]: raise SyntaxError("bad PCX image size") logger.debug("BBox: %s %s %s %s", *bbox) # format version = s[1] bits = s[3] planes = s[65] ignored_stride = i16(s, 66) logger.debug( "PCX version %s, bits %s, planes %s, stride %s", version, bits, planes, ignored_stride, ) self.info["dpi"] = i16(s, 12), i16(s, 14) if bits == 1 and planes == 1: mode = rawmode = "1" elif bits == 1 and planes in (2, 4): mode = "P" rawmode = "P;%dL" % planes self.palette = ImagePalette.raw("RGB", s[16:64]) elif version == 5 and bits == 8 and planes == 1: mode = rawmode = "L" # FIXME: hey, this doesn't work with the incremental loader !!! self.fp.seek(-769, io.SEEK_END) s = self.fp.read(769) if len(s) == 769 and s[0] == 12: # check if the palette is linear greyscale for i in range(256): if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3: mode = rawmode = "P" break if mode == "P": self.palette = ImagePalette.raw("RGB", s[1:]) self.fp.seek(128) elif version == 5 and bits == 8 and planes == 3: mode = "RGB" rawmode = "RGB;L" else: raise OSError("unknown PCX mode") self.mode = mode self._size = bbox[2] - bbox[0], bbox[3] - bbox[1] # don't trust the passed in stride. Calculate for ourselves. # CVE-2020-35655 stride = (self._size[0] * bits + 7) // 8 stride += stride % 2 bbox = (0, 0) + self.size logger.debug("size: %sx%s", *self.size) self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))] ``` ## `src/PIL/PcxImagePlugin.py` — `function:PcxImageFile._open` (lines 52-121) ```python def _open(self): # header s = self.fp.read(128) if not _accept(s): raise SyntaxError("not a PCX file") # image bbox = i16(s, 4), i16(s, 6), i16(s, 8) + 1, i16(s, 10) + 1 if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]: raise SyntaxError("bad PCX image size") logger.debug("BBox: %s %s %s %s", *bbox) # format version = s[1] bits = s[3] planes = s[65] ignored_stride = i16(s, 66) logger.debug( "PCX version %s, bits %s, planes %s, stride %s", version, bits, planes, ignored_stride, ) self.info["dpi"] = i16(s, 12), i16(s, 14) if bits == 1 and planes == 1: mode = rawmode = "1" elif bits == 1 and planes in (2, 4): mode = "P" rawmode = "P;%dL" % planes self.palette = ImagePalette.raw("RGB", s[16:64]) elif version == 5 and bits == 8 and planes == 1: mode = rawmode = "L" # FIXME: hey, this doesn't work with the incremental loader !!! self.fp.seek(-769, io.SEEK_END) s = self.fp.read(769) if len(s) == 769 and s[0] == 12: # check if the palette is linear greyscale for i in range(256): if s[i * 3 + 1 : i * 3 + 4] != o8(i) * 3: mode = rawmode = "P" break if mode == "P": self.palette = ImagePalette.raw("RGB", s[1:]) self.fp.seek(128) elif version == 5 and bits == 8 and planes == 3: mode = "RGB" rawmode = "RGB;L" else: raise OSError("unknown PCX mode") self.mode = mode self._size = bbox[2] - bbox[0], bbox[3] - bbox[1] # don't trust the passed in stride. Calculate for ourselves. # CVE-2020-35655 stride = (self._size[0] * bits + 7) // 8 stride += stride % 2 bbox = (0, 0) + self.size logger.debug("size: %sx%s", *self.size) self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))] ```