From b3d29e946aa4ea468922b00cdc1b41ac788ef263 Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Fri, 13 May 2022 11:33:33 -0600 Subject: [PATCH 1/3] Always use GIF89a for long comments Fix bug that allows GIFs with long comments to be written as GIF87a. --- Tests/test_file_gif.py | 3 ++- src/PIL/GifImagePlugin.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 3c2fab72278..07b5592e8b0 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -804,8 +804,9 @@ def test_comment_over_255(tmp_path): im.info["comment"] = comment im.save(out) with Image.open(out) as reread: - assert reread.info["comment"] == comment + # Test that GIF89a is used for long comment + assert reread.info["version"] == b"GIF89a" def test_zero_comment_subblocks(): diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 9b34a3b0e1b..3376cccaef9 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -915,7 +915,7 @@ def _get_global_header(im, info): for extensionKey in ["transparency", "duration", "loop", "comment"]: if info and extensionKey in info: if (extensionKey == "duration" and info[extensionKey] == 0) or ( - extensionKey == "comment" and not (1 <= len(info[extensionKey]) <= 255) + extensionKey == "comment" and len(info[extensionKey]) == 0 ): continue version = b"89a" From 98329354e0328d1a2206b108b3d3b150821447bc Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 19 May 2022 20:59:16 +1000 Subject: [PATCH 2/3] Simplified version check --- src/PIL/GifImagePlugin.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 3376cccaef9..e8551ca33f9 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -912,17 +912,16 @@ def _get_global_header(im, info): # https://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp version = b"87a" - for extensionKey in ["transparency", "duration", "loop", "comment"]: - if info and extensionKey in info: - if (extensionKey == "duration" and info[extensionKey] == 0) or ( - extensionKey == "comment" and len(info[extensionKey]) == 0 - ): - continue - version = b"89a" - break - else: - if im.info.get("version") == b"89a": - version = b"89a" + if im.info.get("version") == b"89a" or ( + info + and ( + "transparency" in info + or "loop" in info + or info.get("duration") + or info.get("comment") + ) + ): + version = b"89a" background = _get_background(im, info.get("background")) From 138bd280e4768d1e7b59852c72c33ea6019e5c8e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 19 May 2022 20:59:32 +1000 Subject: [PATCH 3/3] Added check to test_comment as well --- Tests/test_file_gif.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 07b5592e8b0..a404387cbec 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -794,6 +794,9 @@ def test_comment(tmp_path): with Image.open(out) as reread: assert reread.info["comment"] == im.info["comment"].encode() + # Test that GIF89a is used for comments + assert reread.info["version"] == b"GIF89a" + def test_comment_over_255(tmp_path): out = str(tmp_path / "temp.gif") @@ -805,7 +808,8 @@ def test_comment_over_255(tmp_path): im.save(out) with Image.open(out) as reread: assert reread.info["comment"] == comment - # Test that GIF89a is used for long comment + + # Test that GIF89a is used for comments assert reread.info["version"] == b"GIF89a"