From c8cb6bcdf009e94c12c6e256b8e24a9bc5fdaf05 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Wed, 24 Oct 2012 23:16:41 -0400 Subject: correct locking in stdio functions that tried to be lock-free these functions must behave as if they obtain the lock via flockfile to satisfy POSIX requirements. since another thread can provably hold the lock when they are called, they must wait to obtain the lock before they can return, even if the correct return value could be obtained without locking. in the case of fclose and freopen, failure to do so could cause correct (albeit obscure) programs to crash or otherwise misbehave; in the case of feof, ferror, and fwide, failure to obtain the lock could sometimes return incorrect results. in any case, having these functions proceed and return while another thread held the lock was wrong. --- src/stdio/fileno.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/stdio/fileno.c') diff --git a/src/stdio/fileno.c b/src/stdio/fileno.c index 9ffb26d5..ba7f9391 100644 --- a/src/stdio/fileno.c +++ b/src/stdio/fileno.c @@ -2,6 +2,11 @@ int fileno(FILE *f) { + /* f->fd never changes, but the lock must be obtained and released + * anyway since this function cannot return while another thread + * holds the lock. */ + FLOCK(f); + FUNLOCK(f); return f->fd; } -- cgit v1.2.1