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/fread.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/stdio/fread.c') diff --git a/src/stdio/fread.c b/src/stdio/fread.c index c461256c..33a65f58 100644 --- a/src/stdio/fread.c +++ b/src/stdio/fread.c @@ -8,11 +8,10 @@ size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f) unsigned char *dest = destv; size_t len = size*nmemb, l = len, k; - /* Never touch the file if length is zero.. */ - if (!l) return 0; - FLOCK(f); + f->mode |= f->mode-1; + if (f->rend - f->rpos > 0) { /* First exhaust the buffer. */ k = MIN(f->rend - f->rpos, l); -- cgit v1.2.1