Raise ValueError if PNG sRGB chunk is truncated

This commit is contained in:
Andrew Murray 2022-07-11 20:02:41 +10:00
parent 4db2ed3a6f
commit 40a0999303
2 changed files with 7 additions and 1 deletions

View File

@ -635,7 +635,9 @@ class TestFilePng:
assert_image_equal_tofile(im, "Tests/images/bw_gradient.png") assert_image_equal_tofile(im, "Tests/images/bw_gradient.png")
@pytest.mark.parametrize("cid", (b"IHDR", b"pHYs", b"acTL", b"fcTL", b"fdAT")) @pytest.mark.parametrize(
"cid", (b"IHDR", b"sRGB", b"pHYs", b"acTL", b"fcTL", b"fdAT")
)
def test_truncated_chunks(self, cid): def test_truncated_chunks(self, cid):
fp = BytesIO() fp = BytesIO()
with PngImagePlugin.PngStream(fp) as png: with PngImagePlugin.PngStream(fp) as png:

View File

@ -509,6 +509,10 @@ class PngStream(ChunkStream):
# 3 absolute colorimetric # 3 absolute colorimetric
s = ImageFile._safe_read(self.fp, length) s = ImageFile._safe_read(self.fp, length)
if length < 1:
if ImageFile.LOAD_TRUNCATED_IMAGES:
return s
raise ValueError("Truncated sRGB chunk")
self.im_info["srgb"] = s[0] self.im_info["srgb"] = s[0]
return s return s