fix stdio lock dependency on read-after-free not faulting
[musl] / src / stdio / ftrylockfile.c
1 #include "stdio_impl.h"
2 #include "pthread_impl.h"
3 #include <limits.h>
4
5 #define MAYBE_WAITERS 0x40000000
6
7 void __do_orphaned_stdio_locks()
8 {
9         FILE *f;
10         for (f=__pthread_self()->stdio_locks; f; f=f->next_locked)
11                 a_store(&f->lock, 0x40000000);
12 }
13
14 void __unlist_locked_file(FILE *f)
15 {
16         if (f->lockcount) {
17                 if (f->next_locked) f->next_locked->prev_locked = f->prev_locked;
18                 if (f->prev_locked) f->prev_locked->next_locked = f->next_locked;
19                 else __pthread_self()->stdio_locks = f->next_locked;
20         }
21 }
22
23 int ftrylockfile(FILE *f)
24 {
25         pthread_t self = __pthread_self();
26         int tid = self->tid;
27         int owner = f->lock;
28         if ((owner & ~MAYBE_WAITERS) == tid) {
29                 if (f->lockcount == LONG_MAX)
30                         return -1;
31                 f->lockcount++;
32                 return 0;
33         }
34         if (owner < 0) f->lock = owner = 0;
35         if (owner || a_cas(&f->lock, 0, tid))
36                 return -1;
37         f->lockcount = 1;
38         f->prev_locked = 0;
39         f->next_locked = self->stdio_locks;
40         if (f->next_locked) f->next_locked->prev_locked = f;
41         self->stdio_locks = f;
42         return 0;
43 }