Skip to content

Commit 3f7ebc6

Browse files
committed
Merge branch 'jk/tempfile-ferror-fclose-confusion'
A caller of tempfile API that uses stdio interface to write to files may ignore errors while writing, which is detected when tempfile is closed (with a call to ferror()). By that time, the original errno that may have told us what went wrong is likely to be long gone and was overwritten by an irrelevant value. close_tempfile() now resets errno to EIO to make errno at least predictable. * jk/tempfile-ferror-fclose-confusion: tempfile: set errno to a known value before calling ferror()
2 parents 9720b30 + 7e8c935 commit 3f7ebc6

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tempfile.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ int close_tempfile(struct tempfile *tempfile)
247247
tempfile->fd = -1;
248248
if (fp) {
249249
tempfile->fp = NULL;
250-
err = ferror(fp);
251-
err |= fclose(fp);
250+
if (ferror(fp)) {
251+
err = -1;
252+
if (!fclose(fp))
253+
errno = EIO;
254+
} else {
255+
err = fclose(fp);
256+
}
252257
} else {
253258
err = close(fd);
254259
}

0 commit comments

Comments
 (0)