Bug 11216 - fmemopen returns NULL when len=0
: fmemopen returns NULL when len=0
Status: REOPENED
Product: glibc
Classification: Unclassified
Component: stdio
: 2.10
: P2 normal
: ---
Assigned To: Ulrich Drepper
:
:
:
:
  Show dependency treegraph
 
Reported: 2010-01-24 11:35 UTC by Alex Shinn
Modified: 2012-04-28 03:21 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Shinn 2010-01-24 11:35:52 UTC
The following program prints "(nil)" demonstrating the problem:

  int main (int argc, char **argv) {
    char buf[] = "";
    FILE *in = fmemopen(buf, 0, "r");
    fprintf(stderr, "%p\n", in);
  }

As a general-purpose utility for converting arbitrary strings
to streams it makes no more sense to forbid empty strings
(which would simply return EOF immediately) than it does to
forbid empty files or /dev/null.

[However, since it doesn't make sense to have an output stream
with an empty buffer, it seems reasonable to continue to return
NULL for the "w"rite case.]

In the meantime, as a workaround in my own applications I will
wrap fmemopen to open /dev/null in this case.
Comment 1 Dmitry V. Levin 2010-01-24 21:49:55 UTC
(In reply to comment #0)
> [However, since it doesn't make sense to have an output stream
> with an empty buffer, it seems reasonable to continue to return
> NULL for the "w"rite case.]

It might have a sense, too: every write would fail with ENOSPC, like every write
to /dev/full does.
Comment 2 Ulrich Drepper 2010-02-04 12:05:10 UTC
You want an existing interface to be changed just to fit what you think is best?
 Regardless of the impact on existing applications?

There won't be any change.  The implementation is as it is.  Perhaps not optimal
but nobody can prove there are no applications which depend on the existing
semantic.
Comment 3 Rich Felker 2011-09-05 02:06:05 UTC
Nowadays fmemopen is specified by POSIX 2008, and POSIX 2008 has no language
that it shall or may fail if the size is 0. I think this bug should be
reconsidered.
Comment 4 Michael Kerrisk 2012-04-28 02:29:48 UTC
Adding Eric Blake to the CC, since he may be interested in this bug from a
POSIX perspective.
Comment 5 Michael Kerrisk 2012-04-28 03:21:41 UTC
I've added the following text to the fmemopen(3) man page:

[[
If
.I size
is specified as zero,
.BR fmemopen ()
fails with the error
.BR EINVAL .
.\" FIXME http://sourceware.org/bugzilla/show_bug.cgi?id=11216
It would be more consistent if this case successfully created
a stream that then returned end of file on the first attempt at reading.
]]