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/fwide.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/stdio/fwide.c') diff --git a/src/stdio/fwide.c b/src/stdio/fwide.c index f4da47f6..48480685 100644 --- a/src/stdio/fwide.c +++ b/src/stdio/fwide.c @@ -5,6 +5,8 @@ int fwide(FILE *f, int mode) { - if (!f->mode) f->mode = NORMALIZE(mode); - return f->mode; + FLOCK(f); + if (!f->mode) mode = f->mode = NORMALIZE(mode); + FUNLOCK(f); + return mode; } -- cgit v1.2.1