author | Rich Felker <dalias@aerifal.cx> | 2018年11月02日 12:31:19 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2018年11月02日 12:31:19 -0400 |
commit | 4a086030264f5cf423ea76453ef721e2c8e2e093 (patch) | |
tree | 8e09c785e7715b22a42189793c596881dfcfa6cd /src/stdio/fclose.c | |
parent | 00bd3b7d3006c5d350959c994fa65358bf65e6a2 (diff) | |
download | musl-4a086030264f5cf423ea76453ef721e2c8e2e093.tar.gz |
-rw-r--r-- | src/stdio/fclose.c | 32 |
diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c index 889b96d2..d594532b 100644 --- a/src/stdio/fclose.c +++ b/src/stdio/fclose.c @@ -7,26 +7,32 @@ weak_alias(dummy, __unlist_locked_file); int fclose(FILE *f) { int r; - int perm; FLOCK(f); + r = fflush(f); + r |= f->close(f); + FUNLOCK(f); - __unlist_locked_file(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 (!(perm = f->flags & F_PERM)) { - 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(); - } + if (f->flags & F_PERM) return r; - r = fflush(f); - r |= f->close(f); + __unlist_locked_file(f); + + 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(); free(f->getln_buf); - if (!perm) free(f); - else FUNLOCK(f); + free(f); return r; } |