This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH 7/6][BZ #11216] Return fmemopen stream when len==0.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: libc-alpha at sourceware dot org
- Date: Sat, 1 Jun 2013 18:23:08 +0200
- Subject: [PATCH 7/6][BZ #11216] Return fmemopen stream when len==0.
- References: <1370099488-13916-1-git-send-email-neleai at seznam dot cz>
As previous patches changed fmemopen behavior a lot it may be rigth
time to fix that when len==0 fmemopen returns NULL. See:
http://sourceware.org/bugzilla/show_bug.cgi?id=11216
I could get this behavior by setting binmode flag.
Comments?
* libio/fmemopen.c (fmemopen): Return stream when len==0.
>From a74de5327710f46a9921f28d13a678d2ad53d251 Mon Sep 17 00:00:00 2001
From: Ondrej Bilka <neleai@seznam.cz>
Date: Sat, 1 Jun 2013 18:10:35 +0200
Subject: [PATCH] fmemopen
---
libio/fmemopen.c | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/libio/fmemopen.c b/libio/fmemopen.c
index 42632c2..9dfabc6 100644
--- a/libio/fmemopen.c
+++ b/libio/fmemopen.c
@@ -203,19 +203,13 @@ fmemopen (void *buf, size_t len, const char *mode)
cookie_io_functions_t iof;
fmemopen_cookie_t *c;
- if (__glibc_unlikely (len == 0))
- {
- __set_errno (EINVAL);
- return NULL;
- }
-
c = (fmemopen_cookie_t *) malloc (sizeof (fmemopen_cookie_t));
if (c == NULL)
return NULL;
if (buf == NULL)
{
- buf = (char *) malloc (len);
+ buf = (char *) malloc (len + 1);
if (buf == NULL)
{
free (c);
@@ -238,7 +232,7 @@ fmemopen (void *buf, size_t len, const char *mode)
c->pos = 0;
c->size = len;
c->append = (mode[0] == 'a');
- if (c->mybuffer || mode[0] == 'w')
+ if ((c->mybuffer || mode[0] == 'w') && len != 0)
c->buffer[0] = '\0';
c->maxpos = strnlen (c->buffer, len);
@@ -253,6 +247,10 @@ fmemopen (void *buf, size_t len, const char *mode)
c->pos = 0;
}
+ /* We need to set binary mode to cause fmemopen_write always fail. */
+ if (__glibc_unlikely (len == 0))
+ c->binmode = 1;
+
iof.read = fmemopen_read;
iof.write = fmemopen_write;
iof.seek = fmemopen_seek;
--
1.7.10.4