From 58d5a73fac7b4084fe4ba66a1a2c4ed097d0d557 Mon Sep 17 00:00:00 2001 From: Yay295 Date: Mon, 17 Jun 2024 09:29:14 -0500 Subject: [PATCH 1/2] raise SyntaxError when parsing codestream rather than returning an empty string Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/Jpeg2KImagePlugin.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/PIL/Jpeg2KImagePlugin.py b/src/PIL/Jpeg2KImagePlugin.py index 39eb1c203..e50cd7799 100644 --- a/src/PIL/Jpeg2KImagePlugin.py +++ b/src/PIL/Jpeg2KImagePlugin.py @@ -97,7 +97,7 @@ class BoxReader: return tbox -def _parse_codestream(fp): +def _parse_codestream(fp) -> tuple[tuple[int, int], str]: """Parse the JPEG 2000 codestream to extract the size and component count from the SIZ marker segment, returning a PIL (size, mode) tuple.""" @@ -122,7 +122,8 @@ def _parse_codestream(fp): elif csiz == 4: mode = "RGBA" else: - mode = "" + msg = "unable to determine J2K image mode" + raise SyntaxError(msg) return size, mode @@ -237,10 +238,6 @@ class Jpeg2KImageFile(ImageFile.ImageFile): msg = "not a JPEG 2000 file" raise SyntaxError(msg) - if self.size is None or not self.mode: - msg = "unable to determine size/mode" - raise SyntaxError(msg) - self._reduce = 0 self.layers = 0 From 88b21e725432d418e56bdf0721f8581d14a24e40 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Tue, 25 Jun 2024 21:38:47 +1000 Subject: [PATCH 2/2] Added test --- Tests/images/unknown_mode.j2k | Bin 0 -> 318 bytes Tests/test_file_jpeg2k.py | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 Tests/images/unknown_mode.j2k diff --git a/Tests/images/unknown_mode.j2k b/Tests/images/unknown_mode.j2k new file mode 100644 index 0000000000000000000000000000000000000000..38719fe2561bb1341b69ed96e63f23bfb2c45d58 GIT binary patch literal 318 zcmezG|38pHlK})cpcu>n5-d;*;j{8HGX4)@-~sX&8JJjD7#RP@FmO3EbaYGrqW>uj zstktDsYS(^`FRRPdM0`X|0ghTfsHc&YM*@mj{y%C z&*TFPKN$`%2pnLLWnpJ#2J#pWFjxuje6;1!2CHWT>M%yAuLr4Lz#z||z{q}pp+?Dp z!AgXIp@Yf&e?50US3Ot@6HtpOiWZ>pKx8N2z|717u|m*+fun)v&wOrsUWg`Upe8ef jCVQaa3P6|eFfc!4VPpo{^^*x=R||JSd_8wE None: assert im.getpixel((5, 5)) == 31 +def test_unknown_j2k_mode() -> None: + with pytest.raises(UnidentifiedImageError): + with Image.open("Tests/images/unknown_mode.j2k"): + pass + + def test_unbound_local() -> None: # prepatch, a malformed jp2 file could cause an UnboundLocalError exception. with pytest.raises(OSError):