about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-07-16 21:24:02 -0400
committerRich Felker <dalias@aerifal.cx>2011-07-16 21:24:02 -0400
commit94a0171d807dc94302d6505041fc58879c27f3bd (patch)
tree84790261ea40496a5fc8649bda75bad5db5c1d47
parent47d027ee1a44829819c345287623fe75374893ab (diff)
downloadmusl-94a0171d807dc94302d6505041fc58879c27f3bd.tar.gz
musl-94a0171d807dc94302d6505041fc58879c27f3bd.tar.xz
musl-94a0171d807dc94302d6505041fc58879c27f3bd.zip
fix logic error in fread
fread was calling f->read without checking that the file was in
reading mode. this could:
1. crash, if f->read was a null pointer
2. cause unwanted blocking on a terminal already at eof
3. allow reading on a write-only file
-rw-r--r--src/stdio/fread.c7
1 files changed, 1 insertions, 6 deletions
diff --git a/src/stdio/fread.c b/src/stdio/fread.c
index 8105fe99..5c235777 100644
--- a/src/stdio/fread.c
+++ b/src/stdio/fread.c
@@ -21,14 +21,9 @@ size_t fread(void *destv, size_t size, size_t nmemb, FILE *f)
 		l -= k;
 	}
 	
-	if (!l) {
-		FUNLOCK(f);
-		return nmemb;
-	}
-
 	/* Read the remainder directly */
 	for (; l; l-=k, dest+=k) {
-		k = f->read(f, dest, l);
+		k = __toread(f) ? 0 : f->read(f, dest, l);
 		if (k+1<=1) {
 			FUNLOCK(f);
 			return (len-l)/size;