diff options
| author | lukem <lukem@NetBSD.org> | 2001-01-25 02:06:25 +0000 |
|---|---|---|
| committer | lukem <lukem@NetBSD.org> | 2001-01-25 02:06:25 +0000 |
| commit | fa11ebb1334320acf15ea44bcff6e11bd76b16ab (patch) | |
| tree | 451eccea8ab5835376d923e7baa2a7b2dbc365f3 /lib | |
| parent | 7df82ad12b3521804f4402793e70661e67e8b1ec (diff) | |
apparently ansi c only required fread(3) to return 0 if size or nmembs == 0.
however, susv2 adds the same to fwrite(3), so add the explicit check.
document this for both fread & fwrite. move diagassert for buf!=NULL to after
the (size * nmembs) == 0 check.
this has the helpful side effect of preventing the _DIAGASSERT()ion in
fwrite() being triggered by lots of 3rdparty code that calls fwrite() with
buf=NULL count=0
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/libc/stdio/fread.3 | 9 | ||||
| -rw-r--r-- | lib/libc/stdio/fread.c | 13 | ||||
| -rw-r--r-- | lib/libc/stdio/fwrite.c | 13 |
3 files changed, 24 insertions, 11 deletions
diff --git a/lib/libc/stdio/fread.3 b/lib/libc/stdio/fread.3 index 0e2ffa69ab1..0f08bdbe5f4 100644 --- a/lib/libc/stdio/fread.3 +++ b/lib/libc/stdio/fread.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: fread.3,v 1.7 2000/12/29 15:22:50 kleink Exp $ +.\" $NetBSD: fread.3,v 1.8 2001/01/25 02:06:25 lukem Exp $ .\" .\" Copyright (c) 1990, 1991, 1993 .\" The Regents of the University of California. All rights reserved. @@ -82,6 +82,13 @@ 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, the functions return 0 and the state of +.Fa stream +remains unchanged. 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 9d83c395565..5e2eac61c1d 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -1,4 +1,4 @@ -/* $NetBSD: fread.c,v 1.13 1999/09/20 04:39:28 lukem Exp $ */ +/* $NetBSD: fread.c,v 1.14 2001/01/25 02:06:25 lukem Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)fread.c 8.2 (Berkeley) 12/11/93"; #else -__RCSID("$NetBSD: fread.c,v 1.13 1999/09/20 04:39:28 lukem Exp $"); +__RCSID("$NetBSD: fread.c,v 1.14 2001/01/25 02:06:25 lukem Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -63,16 +63,17 @@ fread(buf, size, count, fp) int r; size_t total; - _DIAGASSERT(buf != NULL); _DIAGASSERT(fp != NULL); - /* * 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. + * or a size of 0. Whilst ANSI imposes no such requirements on + * fwrite, the SUSv2 does. */ if ((resid = count * size) == 0) return (0); + + _DIAGASSERT(buf != NULL); + FLOCKFILE(fp); if (fp->_r < 0) fp->_r = 0; diff --git a/lib/libc/stdio/fwrite.c b/lib/libc/stdio/fwrite.c index 0d45807bd2e..92818b2c512 100644 --- a/lib/libc/stdio/fwrite.c +++ b/lib/libc/stdio/fwrite.c @@ -1,4 +1,4 @@ -/* $NetBSD: fwrite.c,v 1.12 1999/09/20 04:39:30 lukem Exp $ */ +/* $NetBSD: fwrite.c,v 1.13 2001/01/25 02:06:25 lukem Exp $ */ /*- * Copyright (c) 1990, 1993 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)fwrite.c 8.1 (Berkeley) 6/4/93"; #else -__RCSID("$NetBSD: fwrite.c,v 1.12 1999/09/20 04:39:30 lukem Exp $"); +__RCSID("$NetBSD: fwrite.c,v 1.13 2001/01/25 02:06:25 lukem Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -66,12 +66,17 @@ fwrite(buf, size, count, fp) struct __suio uio; struct __siov iov; - _DIAGASSERT(buf != NULL); _DIAGASSERT(fp != NULL); + /* + * SUSv2 requires a return value of 0 for a count or a size of 0. + */ + if ((n = count * size) == 0) + return (0); + _DIAGASSERT(buf != NULL); /* LINTED we don't play with buf */ 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; |
