path: root/src/stdio/fclose.c
diff options
authorRich Felker <>2012-10-24 23:16:41 -0400
committerRich Felker <>2012-10-24 23:16:41 -0400
commitc8cb6bcdf009e94c12c6e256b8e24a9bc5fdaf05 (patch)
tree28d9441f99c5449267c987cb46b6610dc244a991 /src/stdio/fclose.c
parent892cafff665b44d238e3b664f61ca38dd965cba6 (diff)
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.
Diffstat (limited to 'src/stdio/fclose.c')
1 files changed, 5 insertions, 2 deletions
diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c
index 8fdc3f7d..92bf7ff8 100644
--- a/src/stdio/fclose.c
+++ b/src/stdio/fclose.c
@@ -3,9 +3,12 @@
int fclose(FILE *f)
int r;
- int perm = f->flags & F_PERM;
+ int perm;
+ /* This lock is not paired with any unlock. */
+ FLOCK(f);
- if (!perm) {
+ if (!(perm = f->flags & F_PERM)) {
if (f->prev) f->prev->next = f->next;
if (f->next) f->next->prev = f->prev;