From 6e2bb7acf42589fb7130b039d0623e2ca42503dd Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 4 Sep 2014 22:21:17 -0400 Subject: fix multiple stdio functions' behavior on zero-length operations previously, fgets, fputs, fread, and fwrite completely omitted locking and access to the FILE object when their arguments yielded a zero length read or write operation independent of the FILE state. this optimization was invalid; it wrongly skipped marking the stream as byte-oriented (a C conformance bug) and exposed observably missing synchronization (a POSIX conformance bug) where one of these functions could wrongly complete despite another thread provably holding the lock. --- src/stdio/fgets.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/stdio/fgets.c') diff --git a/src/stdio/fgets.c b/src/stdio/fgets.c index cf5b1039..d3f9819e 100644 --- a/src/stdio/fgets.c +++ b/src/stdio/fgets.c @@ -10,14 +10,16 @@ char *fgets(char *restrict s, int n, FILE *restrict f) size_t k; int c; + FLOCK(f); + if (n--<=1) { + f->mode |= f->mode-1; + FUNLOCK(f); if (n) return 0; *s = 0; return s; } - FLOCK(f); - while (n) { z = memchr(f->rpos, '\n', f->rend - f->rpos); k = z ? z - f->rpos + 1 : f->rend - f->rpos; -- cgit v1.2.1