diff options
author | gahr <gahr@FreeBSD.org> | 2014-06-02 13:48:57 +0000 |
---|---|---|
committer | gahr <gahr@FreeBSD.org> | 2014-06-02 13:48:57 +0000 |
commit | 1a11db3c8dca40b3c1a7c96e7b370c6025ba15c2 (patch) | |
tree | bb95f8d1cc550f8ec2eefb7e5950f9675a4e62c5 /lib | |
parent | 912807542213a4554df220d98738944a5449e153 (diff) | |
download | FreeBSD-src-1a11db3c8dca40b3c1a7c96e7b370c6025ba15c2.zip FreeBSD-src-1a11db3c8dca40b3c1a7c96e7b370c6025ba15c2.tar.gz |
- Return NULL and set errno to EINVAL if size is 0 (as required by POSIX).
Update the manpage to reflect this change.
- Always set the current position to the first null-byte when opening in append
mode. This makes the implementation compatible with glibc's. Update the test
suite.
Reported by: pho
Approved by: cognet
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/stdio/fmemopen.c | 17 | ||||
-rw-r--r-- | lib/libc/stdio/fopen.3 | 9 |
2 files changed, 18 insertions, 8 deletions
diff --git a/lib/libc/stdio/fmemopen.c b/lib/libc/stdio/fmemopen.c index 581a91e..ebd3596 100644 --- a/lib/libc/stdio/fmemopen.c +++ b/lib/libc/stdio/fmemopen.c @@ -57,6 +57,14 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode) int flags, rc; /* + * POSIX says we shall return EINVAL if size is 0. + */ + if (size == 0) { + errno = EINVAL; + return (NULL); + } + + /* * Retrieve the flags as used by open(2) from the mode argument, and * validate them. */ @@ -119,14 +127,7 @@ fmemopen(void * __restrict buf, size_t size, const char * __restrict mode) */ switch (mode[0]) { case 'a': - if (ck->bin) { - /* - * This isn't useful, since the buffer isn't allowed - * to grow. - */ - ck->off = ck->len = size; - } else - ck->off = ck->len = strnlen(ck->buf, ck->size); + ck->off = ck->len = strnlen(ck->buf, ck->size); break; case 'r': ck->len = size; diff --git a/lib/libc/stdio/fopen.3 b/lib/libc/stdio/fopen.3 index f11f4e0..b39cb5a 100644 --- a/lib/libc/stdio/fopen.3 +++ b/lib/libc/stdio/fopen.3 @@ -302,6 +302,15 @@ for any of the errors specified for the routines .Xr fclose 3 and .Xr fflush 3 . +.Pp +The +.Fn fmemopen +function +may also fail and set +.Va errno +if the +.Fa size +argument is 0. .Sh SEE ALSO .Xr open 2 , .Xr fclose 3 , |