about summary refs log tree commit diff
path: root/libio/fmemopen.c
diff options
context:
space:
mode:
authorPaul Pluzhnikov <ppluzhnikov@google.com>2015-08-12 23:51:04 -0700
committerPaul Pluzhnikov <ppluzhnikov@google.com>2015-08-12 23:51:04 -0700
commit63e952d9be87db68f0e4164d4a5760b32e77ebff (patch)
tree652f8bece6a6a04f7618093c9d3dc1b0a398d965 /libio/fmemopen.c
parent8a29509dd9aa179bfe4ef96d49d72f6816ec878f (diff)
downloadglibc-63e952d9be87db68f0e4164d4a5760b32e77ebff.tar.gz
glibc-63e952d9be87db68f0e4164d4a5760b32e77ebff.tar.xz
glibc-63e952d9be87db68f0e4164d4a5760b32e77ebff.zip
Fix BZ #18820 -- fmemopen may leak memory on failure.
Diffstat (limited to 'libio/fmemopen.c')
-rw-r--r--libio/fmemopen.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/libio/fmemopen.c b/libio/fmemopen.c
index 3ab3e8dac4..66e2d833ee 100644
--- a/libio/fmemopen.c
+++ b/libio/fmemopen.c
@@ -149,6 +149,7 @@ __fmemopen (void *buf, size_t len, const char *mode)
 {
   cookie_io_functions_t iof;
   fmemopen_cookie_t *c;
+  FILE *result;
 
   c = (fmemopen_cookie_t *) calloc (sizeof (fmemopen_cookie_t), 1);
   if (c == NULL)
@@ -209,7 +210,16 @@ __fmemopen (void *buf, size_t len, const char *mode)
   iof.seek = fmemopen_seek;
   iof.close = fmemopen_close;
 
-  return _IO_fopencookie (c, mode, iof);
+  result = _IO_fopencookie (c, mode, iof);
+  if (__glibc_unlikely (result == NULL))
+    {
+      if (c->mybuffer)
+	free (c->buffer);
+
+      free (c);
+    }
+
+  return result;
 }
 libc_hidden_def (__fmemopen)
 versioned_symbol (libc, __fmemopen, fmemopen, GLIBC_2_22);