summaryrefslogtreecommitdiff
path: root/lib/libc
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2009-07-12 18:45:22 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2009-07-12 18:45:22 +0000
commit35b45cf211de27e6932252ed9c60e7a1f6887998 (patch)
treecfb0de6f429d27ebed30971eea3efd070571053e /lib/libc
parent3b609b2042d45ba683a407506561ad0b511e6f93 (diff)
fwrite() should also return 0 if either size or nmemb are 0.
Adapted from FreeBSD. OK deraadt@
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdio/fread.314
-rw-r--r--lib/libc/stdio/fread.c6
-rw-r--r--lib/libc/stdio/fwrite.c10
3 files changed, 22 insertions, 8 deletions
diff --git a/lib/libc/stdio/fread.3 b/lib/libc/stdio/fread.3
index 972d7b2e996..754636f8d01 100644
--- a/lib/libc/stdio/fread.3
+++ b/lib/libc/stdio/fread.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: fread.3,v 1.4 2007/05/31 19:19:31 jmc Exp $
+.\" $OpenBSD: fread.3,v 1.5 2009/07/12 18:45:21 millert Exp $
.\"
.\" Copyright (c) 1990, 1991, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -31,7 +31,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd $Mdocdate: May 31 2007 $
+.Dd $Mdocdate: July 12 2009 $
.Dt FREAD 3
.Os
.Sh NAME
@@ -74,6 +74,16 @@ and
advance the file position indicator for the stream
by the number of bytes read or written.
They return the number of objects read or written.
+If
+.Fa size
+or
+.Fa nmemb
+is 0,
+.Fn fread
+and
+.Fn fwrite
+return 0 with no change made to the
+.Fa stream .
If an error occurs, or the end-of-file is reached,
the return value is a short object count (or zero).
.Pp
diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c
index 62906216d8b..c7389e76ed6 100644
--- a/lib/libc/stdio/fread.c
+++ b/lib/libc/stdio/fread.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fread.c,v 1.6 2005/08/08 08:05:36 espie Exp $ */
+/* $OpenBSD: fread.c,v 1.7 2009/07/12 18:45:21 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -44,9 +44,7 @@ fread(void *buf, size_t size, size_t count, FILE *fp)
size_t total;
/*
- * The ANSI standard requires a return value of 0 for a count
- * or a size of 0. Peculiarily, it imposes no such requirements
- * on fwrite; it only requires fread to be broken.
+ * ANSI and SUSv2 require a return value of 0 if size or count are 0.
*/
if ((resid = count * size) == 0)
return (0);
diff --git a/lib/libc/stdio/fwrite.c b/lib/libc/stdio/fwrite.c
index 8a508dcdba7..a60f9e0807a 100644
--- a/lib/libc/stdio/fwrite.c
+++ b/lib/libc/stdio/fwrite.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fwrite.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */
+/* $OpenBSD: fwrite.c,v 1.6 2009/07/12 18:45:21 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@@ -46,8 +46,14 @@ fwrite(const void *buf, size_t size, size_t count, FILE *fp)
struct __suio uio;
struct __siov iov;
+ /*
+ * ANSI and SUSv2 require a return value of 0 if size or count are 0.
+ */
+ if ((n = count * size) == 0)
+ return (0);
+
iov.iov_base = (void *)buf;
- uio.uio_resid = iov.iov_len = n = count * size;
+ uio.uio_resid = iov.iov_len = n;
uio.uio_iov = &iov;
uio.uio_iovcnt = 1;