about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2014-09-04 22:21:17 -0400
committerRich Felker <dalias@aerifal.cx>2014-09-04 22:21:17 -0400
commit6e2bb7acf42589fb7130b039d0623e2ca42503dd (patch)
tree7af4719cfa84bc8cf0c6fa73fdca42035eaf03e9 /src
parent402611c3ba3be5b3b0486835d98e22ac7ced2722 (diff)
downloadmusl-6e2bb7acf42589fb7130b039d0623e2ca42503dd.tar.gz
musl-6e2bb7acf42589fb7130b039d0623e2ca42503dd.tar.xz
musl-6e2bb7acf42589fb7130b039d0623e2ca42503dd.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/stdio/fgets.c6
-rw-r--r--src/stdio/fputs.c4
-rw-r--r--src/stdio/fread.c5
-rw-r--r--src/stdio/fwrite.c1
4 files changed, 7 insertions, 9 deletions
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;
diff --git a/src/stdio/fputs.c b/src/stdio/fputs.c
index 1112b192..4737f448 100644
--- a/src/stdio/fputs.c
+++ b/src/stdio/fputs.c
@@ -3,9 +3,7 @@
 
 int fputs(const char *restrict s, FILE *restrict f)
 {
-	size_t l = strlen(s);
-	if (!l) return 0;
-	return (int)fwrite(s, l, 1, f) - 1;
+	return (int)fwrite(s, strlen(s), 1, f) - 1;
 }
 
 weak_alias(fputs, fputs_unlocked);
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);
diff --git a/src/stdio/fwrite.c b/src/stdio/fwrite.c
index d5f6542d..81ec271e 100644
--- a/src/stdio/fwrite.c
+++ b/src/stdio/fwrite.c
@@ -28,7 +28,6 @@ size_t __fwritex(const unsigned char *restrict s, size_t l, FILE *restrict f)
 size_t fwrite(const void *restrict src, size_t size, size_t nmemb, FILE *restrict f)
 {
 	size_t k, l = size*nmemb;
-	if (!l) return l;
 	FLOCK(f);
 	k = __fwritex(src, l, f);
 	FUNLOCK(f);