From 77822acff7bf72c4a8e4020fb9256372f851c65e Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Sun, 12 Jul 2009 13:09:43 +0000 Subject: [PATCH] Fix fwrite() to return 0 when size or nmemb are zero. Right now nmemb is returned when size is 0. In newer versions of the standards, it is explicitly required that fwrite() should return 0. Submitted by: Christoph Mallon Approved by: re (kib) --- lib/libc/stdio/fread.c | 4 +--- lib/libc/stdio/fwrite.c | 9 ++++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 851713be461..6253856b9e3 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -67,9 +67,7 @@ __fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict 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 999d5958193..cf52e42f615 100644 --- a/lib/libc/stdio/fwrite.c +++ b/lib/libc/stdio/fwrite.c @@ -57,8 +57,15 @@ fwrite(buf, size, count, fp) struct __suio uio; struct __siov iov; + /* + * ANSI and SUSv2 require a return value of 0 if size or count are 0. + */ + n = count * size; + if (n == 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;