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

Do not close provided file handles with libtiff #7199

Merged
merged 2 commits into from Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 1 addition & 9 deletions src/PIL/TiffImagePlugin.py
Expand Up @@ -1253,9 +1253,8 @@ def _load_libtiff(self):
# To be nice on memory footprint, if there's a
# file descriptor, use that instead of reading
# into a string in python.
# libtiff closes the file descriptor, so pass in a dup.
try:
fp = hasattr(self.fp, "fileno") and os.dup(self.fp.fileno())
fp = hasattr(self.fp, "fileno") and self.fp.fileno()
# flush the file descriptor, prevents error on pypy 2.4+
# should also eliminate the need for fp.tell
# in _seek
Expand Down Expand Up @@ -1305,18 +1304,11 @@ def _load_libtiff(self):
# UNDONE -- so much for that buffer size thing.
n, err = decoder.decode(self.fp.read())

if fp:
try:
os.close(fp)
except OSError:
pass

self.tile = []
self.readonly = 0

self.load_end()

# libtiff closed the fp in a, we need to close self.fp, if possible
if close_self_fp:
self.fp.close()
self.fp = None # might be shared
Expand Down
6 changes: 5 additions & 1 deletion src/libImaging/TiffDecode.c
Expand Up @@ -720,7 +720,11 @@ ImagingLibTiffDecode(
}

decode_err:
TIFFClose(tiff);
if (clientstate->fp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a comment here explaining why we're checking this would be good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've added some comments.

TIFFCleanup(tiff);
} else {
TIFFClose(tiff);
}
TRACE(("Done Decoding, Returning \n"));
// Returning -1 here to force ImageFile.load to break, rather than
// even think about looping back around.
Expand Down