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 when saving #8458

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
if hasattr(fp, "fileno"):
try:
fp.seek(0)
_fp = os.dup(fp.fileno())
_fp = fp.fileno()
except io.UnsupportedOperation:
pass

Expand Down Expand Up @@ -1943,11 +1943,6 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
fp.write(data)
if errcode:
break
if _fp:
try:
os.close(_fp)
except OSError:
pass
if errcode < 0:
msg = f"encoder error {errcode} when writing image file"
raise OSError(msg)
Expand Down
26 changes: 22 additions & 4 deletions src/libImaging/TiffDecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@
decode_err:
// TIFFClose in libtiff calls tif_closeproc and TIFFCleanup
if (clientstate->fp) {
// Pillow will manage the closing of the file rather than libtiff
// Python will manage the closing of the file rather than libtiff
// So only call TIFFCleanup
TIFFCleanup(tiff);
} else {
Expand Down Expand Up @@ -1008,7 +1008,17 @@
) == -1) {
TRACE(("Encode Error, row %d\n", state->y));
state->errcode = IMAGING_CODEC_BROKEN;
TIFFClose(tiff);

// TIFFClose in libtiff calls tif_closeproc and TIFFCleanup
if (clientstate->fp) {
// Python will manage the closing of the file rather than libtiff
// So only call TIFFCleanup
TIFFCleanup(tiff);
} else {
// When tif_closeproc refers to our custom _tiffCloseProc though,
// that is fine, as it does not close the file
TIFFClose(tiff);

Check warning on line 1020 in src/libImaging/TiffDecode.c

View check run for this annotation

Codecov / codecov/patch

src/libImaging/TiffDecode.c#L1020

Added line #L1020 was not covered by tests
}
if (!clientstate->fp) {
free(clientstate->data);
}
Expand All @@ -1025,14 +1035,22 @@
TRACE(("Error flushing the tiff"));
// likely reason is memory.
state->errcode = IMAGING_CODEC_MEMORY;
TIFFClose(tiff);
if (clientstate->fp) {
TIFFCleanup(tiff);
} else {
TIFFClose(tiff);

Check warning on line 1041 in src/libImaging/TiffDecode.c

View check run for this annotation

Codecov / codecov/patch

src/libImaging/TiffDecode.c#L1039-L1041

Added lines #L1039 - L1041 were not covered by tests
}
if (!clientstate->fp) {
free(clientstate->data);
}
return -1;
}
TRACE(("Closing \n"));
TIFFClose(tiff);
if (clientstate->fp) {
TIFFCleanup(tiff);
} else {
TIFFClose(tiff);
}
// reset the clientstate metadata to use it to read out the buffer.
clientstate->loc = 0;
clientstate->size = clientstate->eof; // redundant?
Expand Down
Loading