Index: lib/libc/stdio/fmemopen.c =================================================================== --- lib/libc/stdio/fmemopen.c (revision 266970) +++ lib/libc/stdio/fmemopen.c (working copy) @@ -57,6 +57,14 @@ 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 @@ */ 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; Index: lib/libc/stdio/fopen.3 =================================================================== --- lib/libc/stdio/fopen.3 (revision 266970) +++ lib/libc/stdio/fopen.3 (working copy) @@ -302,6 +302,15 @@ .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 , Index: tools/regression/lib/libc/stdio/test-fmemopen.c =================================================================== --- tools/regression/lib/libc/stdio/test-fmemopen.c (revision 266969) +++ tools/regression/lib/libc/stdio/test-fmemopen.c (working copy) @@ -138,6 +138,13 @@ /* Close the FILE *. */ rc = fclose(fp); assert(rc == 0); + + /* Open a FILE * using a wrong mode */ + fp = fmemopen(NULL, 512, "r"); + assert(fp == NULL); + + fp = fmemopen(NULL, 512, "w"); + assert(fp == NULL); } void @@ -241,6 +248,44 @@ assert(rc == 0); } +void +test_append_binary_pos() +{ + /* + * For compatibility with other implementations (glibc), we set the + * position to 0 when opening an automatically allocated binary stream + * for appending. + */ + + FILE *fp; + + fp = fmemopen(NULL, 16, "ab+"); + assert(ftell(fp) == 0L); + fclose(fp); + + /* + * Make sure that a pre-allocated buffer behaves correctly. + */ + char buf[] = "Hello"; + fp = fmemopen(buf, sizeof(buf), "ab+"); + assert(ftell(fp) == 5); + fclose(fp); +} + +void +test_size_0() +{ + /* + * POSIX mandates that we return EINVAL if size is 0 + */ + + FILE *fp; + + fp = fmemopen(NULL, 0, "r+"); + assert(fp == NULL); + assert(errno == EINVAL); +} + int main(void) { @@ -248,5 +293,7 @@ test_preexisting(); test_data_length(); test_binary(); + test_append_binary_pos(); + test_size_0(); return (0); }