X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fstdio%2Ffclose.c;h=d594532bd64fdb5695544d67ee4dababd4c9a214;hb=6aeb9c6703670649ee09b3c8575fb428168bb75c;hp=317b3c901e7dfd066bfcb3a236b89c2a8b8e02cd;hpb=5345c9b884e7c4e73eb2c8bb83b8d0df20f95afb;p=musl diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c index 317b3c90..d594532b 100644 --- a/src/stdio/fclose.c +++ b/src/stdio/fclose.c @@ -1,5 +1,5 @@ #include "stdio_impl.h" -#include "libc.h" +#include static void dummy(FILE *f) { } weak_alias(dummy, __unlist_locked_file); @@ -7,25 +7,32 @@ weak_alias(dummy, __unlist_locked_file); int fclose(FILE *f) { int r; - int perm; - FFINALLOCK(f); + FLOCK(f); + r = fflush(f); + r |= f->close(f); + FUNLOCK(f); + + /* Past this point, f is closed and any further explict access + * to it is undefined. However, it still exists as an entry in + * the open file list and possibly in the thread's locked files + * list, if it was closed while explicitly locked. Functions + * which process these lists must tolerate dead FILE objects + * (which necessarily have inactive buffer pointers) without + * producing any side effects. */ + + if (f->flags & F_PERM) return r; __unlist_locked_file(f); - if (!(perm = f->flags & F_PERM)) { - OFLLOCK(); - if (f->prev) f->prev->next = f->next; - if (f->next) f->next->prev = f->prev; - if (libc.ofl_head == f) libc.ofl_head = f->next; - OFLUNLOCK(); - } + FILE **head = __ofl_lock(); + if (f->prev) f->prev->next = f->next; + if (f->next) f->next->prev = f->prev; + if (*head == f) *head = f->next; + __ofl_unlock(); - r = fflush(f); - r |= f->close(f); + free(f->getln_buf); + free(f); - if (f->getln_buf) free(f->getln_buf); - if (!perm) free(f); - return r; }