Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid argument warning #7442

Merged
merged 1 commit into from Oct 13, 2023
Merged

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Oct 4, 2023

I noticed a warning was being raised in tests

Tests/test_file_libtiff.py::TestFileLibTiff::test_multipage
Tests/test_file_libtiff.py::TestFileLibTiff::test_multipage_seek_backwards
Tests/test_file_libtiff.py::TestFileLibTiff::test_page_number_x_0
Tests/test_imagesequence.py::test_libtiff
/opt/hostedtoolcache/Python/3.11.5/x64/lib/python3.11/site-packages/PIL/TiffImagePlugin.py:866: UserWarning: [Errno 22] Invalid argument
warnings.warn(str(msg))

A fuller traceback is

___________________________________________________ TestFileLibTiff.test_multipage _____________________________________________________

self = <Tests.test_file_libtiff.TestFileLibTiff object at 0x108cf1850>

    def test_multipage(self):
        # issue #862
        TiffImagePlugin.READ_LIBTIFF = True
        with Image.open("Tests/images/multipage.tiff") as im:
            # file is a multipage tiff,  10x10 green, 10x10 red, 20x20 blue
    
            im.seek(0)
            assert im.size == (10, 10)
>           assert im.convert("RGB").getpixel((0, 0)) == (0, 128, 0)

Tests/test_file_libtiff.py:59: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
PIL/Image.py:916: in convert
    self.load()
PIL/TiffImagePlugin.py:1205: in load
    return self._load_libtiff()
PIL/TiffImagePlugin.py:1303: in _load_libtiff
    self.load_end()
PIL/TiffImagePlugin.py:1225: in load_end
    ImageOps.exif_transpose(self, in_place=True)
PIL/ImageOps.py:592: in exif_transpose
    image_exif = image.getexif()
PIL/Image.py:1439: in getexif
    self._exif.load_from_fp(self.fp, self.tag_v2._offset)
PIL/Image.py:3753: in load_from_fp
    self._info.load(self.fp)
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <PIL.TiffImagePlugin.ImageFileDirectory_v2 object at 0x108f555b0>, fp = <_io.BufferedReader name='Tests/images/multipage.tiff'>

    def load(self, fp):
        self.reset()
        self._offset = fp.tell()
    
        if True:#try:
            tag_count = (
                self._unpack("Q", self._ensure_read(fp, 8))
                if self._bigtiff
                else self._unpack("H", self._ensure_read(fp, 2))
            )[0]
            for i in range(tag_count):
                tag, typ, count, data = (
                    self._unpack("HHQ8s", self._ensure_read(fp, 20))
                    if self._bigtiff
                    else self._unpack("HHL4s", self._ensure_read(fp, 12))
                )
    
                tagname = TiffTags.lookup(tag, self.group).name
                typname = TYPES.get(typ, "unknown")
                msg = f"tag: {tagname} ({tag}) - type: {typname} ({typ})"
    
                try:
                    unit_size, handler = self._load_dispatch[typ]
                except KeyError:
                    logger.debug("%s - unsupported type %s", msg, typ)
                    continue  # ignore unsupported type
                size = count * unit_size
                if size > (8 if self._bigtiff else 4):
                    here = fp.tell()
                    (offset,) = self._unpack("Q" if self._bigtiff else "L", data)
                    msg += f" Tag Location: {here} - Data Location: {offset}"
                    fp.seek(offset)
                    data = ImageFile._safe_read(fp, size)
>                   fp.seek(here)
E                   OSError: [Errno 22] Invalid argument

where here is a negative number.

Taking inspiration from

# reset buffered io handle in case fp
# was passed to libtiff, invalidating the buffer
self.fp.tell()

I've added self.fp.tell() into Exif.load_from_fp() to fix this.

@hugovk hugovk merged commit ebefb5f into python-pillow:main Oct 13, 2023
53 checks passed
@radarhere radarhere deleted the warning branch October 13, 2023 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants