diff --git a/conftest.py b/conftest.py index 57a5a80db..3000bdd4b 100644 --- a/conftest.py +++ b/conftest.py @@ -4,6 +4,7 @@ import sys from PIL import Image from PIL import PdfParser +from PIL import SpiderImagePlugin pytest_plugins = ["Tests.helper"] @@ -12,6 +13,8 @@ def calculate_coverage(test_name): all_branches = { "branches1": Image.branches, "branches2": PdfParser.XrefTable.branches, + "branches3": SpiderImagePlugin.branches1, + "branches4": SpiderImagePlugin.branches2, # Add more } @@ -47,4 +50,3 @@ def pytest_sessionfinish(session, exitstatus): coverage = calculate_coverage(test_name) print("\nBRANCH COVERAGE for", test_name, ":", coverage, "%\n") - \ No newline at end of file diff --git a/src/PIL/SpiderImagePlugin.py b/src/PIL/SpiderImagePlugin.py index 14aff84e0..e573345de 100644 --- a/src/PIL/SpiderImagePlugin.py +++ b/src/PIL/SpiderImagePlugin.py @@ -42,6 +42,33 @@ from typing import IO, TYPE_CHECKING from . import Image, ImageFile +branches1 = { + "1": False, + "2": False, + "3": False, + "4": False, + "5": False, + "6": False, + "7": False, + "8": False, + "9": False, + "10": False, + "11": False, + "12": False, + "13": False, + "14": False, +} + +branches2 = { + "1": False, + "2": False, + "3": False, + "4": False, + "5": False, + "6": False, + "7": False, +} + def isInt(f): try: i = int(f) @@ -104,35 +131,52 @@ class SpiderImageFile(ImageFile.ImageFile): f = self.fp.read(n) try: + branches1["1"] = True self.bigendian = 1 t = struct.unpack(">27f", f) # try big-endian first hdrlen = isSpiderHeader(t) + if hdrlen == 0: + branches1["2"] = True self.bigendian = 0 t = struct.unpack("<27f", f) # little-endian hdrlen = isSpiderHeader(t) + else: # ADDED FOR BRANCH MARKING + branches1["3"] = True + if hdrlen == 0: + branches1["4"] = True msg = "not a valid Spider file" raise SyntaxError(msg) + else: # ADDED FOR BRANCH MARKING + branches1["5"] = True + except struct.error as e: + branches1["6"] = True msg = "not a valid Spider file" raise SyntaxError(msg) from e h = (99,) + t # add 1 value : spider header index starts at 1 iform = int(h[5]) + if iform != 1: + branches1["7"] = True msg = "not a Spider 2D image" raise SyntaxError(msg) + else: # ADDED FOR BRANCH MARKING + branches1["8"] = True self._size = int(h[12]), int(h[2]) # size in pixels (width, height) self.istack = int(h[24]) self.imgnumber = int(h[27]) if self.istack == 0 and self.imgnumber == 0: + branches1["9"] = True # stk=0, img=0: a regular 2D image offset = hdrlen self._nimages = 1 elif self.istack > 0 and self.imgnumber == 0: + branches1["10"] = True # stk>0, img=0: Opening the stack for the first time self.imgbytes = int(h[12]) * int(h[2]) * 4 self.hdrlen = hdrlen @@ -141,16 +185,20 @@ class SpiderImageFile(ImageFile.ImageFile): offset = hdrlen * 2 self.imgnumber = 1 elif self.istack == 0 and self.imgnumber > 0: + branches1["11"] = True # stk=0, img>0: an image within the stack offset = hdrlen + self.stkoffset self.istack = 2 # So Image knows it's still a stack else: + branches1["12"] = True msg = "inconsistent stack header values" raise SyntaxError(msg) if self.bigendian: + branches1["13"] = True self.rawmode = "F;32BF" else: + branches1["14"] = True self.rawmode = "F;32F" self._mode = "F" @@ -174,10 +222,18 @@ class SpiderImageFile(ImageFile.ImageFile): def seek(self, frame: int) -> None: if self.istack == 0: + branches2["1"] = True msg = "attempt to seek in a non-stack file" raise EOFError(msg) + else: # ADDED FOR BRANCH MARKING + branches2["2"] = True + if not self._seek_check(frame): + branches2["3"] = True return + else: # ADDED FOR BRANCH MARKING + branches2["4"] = True + self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes) self.fp = self._fp self.fp.seek(self.stkoffset)