This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug stdio/21544] New: stdio fprintf, printf returning >0 after underlying fd closed
- From: "dslowik500 at gmail dot com" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sourceware dot org
- Date: Sat, 03 Jun 2017 21:57:14 +0000
- Subject: [Bug stdio/21544] New: stdio fprintf, printf returning >0 after underlying fd closed
- Auto-submitted: auto-generated
https://sourceware.org/bugzilla/show_bug.cgi?id=21544
Bug ID: 21544
Summary: stdio fprintf, printf returning >0 after underlying fd
closed
Product: glibc
Version: 2.23
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: stdio
Assignee: unassigned at sourceware dot org
Reporter: dslowik500 at gmail dot com
Target Milestone: ---
As in the code below,
After opening a regular file FILE *fp to write to, then closing the underlying
file descriptor, a call to fprintf on fp returns number of chars given to
fprintf. I think it should return -1 as an strace shows that first fprintf
seems to trigger a call to fstat on the underlying fd which fails with EBADF
errno. fprintf will return -1 only after enough chars have been added into the
stdio buffer to trigger a write syscall, and that write error is propagated
back through fprintf, but not the initial fstat error. The question is: Should
fprintf report the error when it first detects it through fstat or wait until
write later reports the same error?
[Note that if the fd is closed 'after' first fprintf call, it will have to wait
for a full stdio buffer to trigger write to detect error; but I'm thinking
first error detected should be reported by stdio in either case.]
A similar thing happens with printf writing to stdout after closing
STDOUT_FILENO. That code and discussion is at:
https://stackoverflow.com/questions/44034025/stdio-to-terminal-after-closestdout-fileno-behavior
OS:
Linux HAL 4.4.0-79-generic #100-Ubuntu SMP Wed May 17 19:58:14 UTC 2017 x86_64
x86_64 x86_64 GNU/Linux
compiler:
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
code for fprintf to regular file:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main() {
FILE *fp = fopen("out.txt", "w");
// Close underlying file descriptor:
int fd = fileno(fp);
close(fd);
// write fails:
if (write(fd, "Direct write\n", 13) != 13)
fprintf(stderr, "Error on write after close(fd): %s\n", strerror(errno));
// but fprintf() calls continue fine, ferror(fp) = 0, until first write()
call on filled stdio buffer:
int rtn = 1, cum = 0;
errno = 0;
// while (rtn>0) { // uncomment while loop to fill stdio buffer.
if ((rtn = fprintf(fp, "fprintf after close(fd)\n")) < 0 || ferror(stdout))
fprintf(stderr, "Error on fprintf after close(fp): %s\n",
strerror(errno));
cum += rtn;
fprintf(stderr, "fprintf returned %d, cum: %d\n", rtn, cum);
// }
// Only on fflush is error detected (if write not yet triggered above):
if (fflush(fp) || ferror(fp))
fprintf(stderr, "Error on fflush(fp): %s\n", strerror(errno));
}
--
You are receiving this mail because:
You are on the CC list for the bug.