From 21751a14bea4e6794c9941fad74d8116b43ea586 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@k1024.org>
Date: Fri, 1 May 2015 00:43:16 +0200
Subject: [PATCH] Fix memory leak on get_all OutOfMemory handling path

It's unlikely that the situation is recoverable when failing to
allocate memory, but the current code is clearly buggy: PyMem_Realloc
doesn't clobber the existing buffer on failure, so not deallocating it
will result in a leak.
---
 xattr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/xattr.c b/xattr.c
index 7c59090..a966431 100644
--- a/xattr.c
+++ b/xattr.c
@@ -450,7 +450,7 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds)
     PyObject *myarg, *res;
     int nofollow=0;
     const char *ns = NULL;
-    char *buf_list, *buf_val;
+    char *buf_list, *buf_val, *buf_val_tmp;
     const char *s;
     ssize_t nalloc, nlist, nval;
     PyObject *mylist;
@@ -523,10 +523,12 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds)
             if(nval == -1) {
                 if(errno == ERANGE) {
                     nval = _get_obj(&tgt, s, NULL, 0);
-                    if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
+                    if((buf_val_tmp = PyMem_Realloc(buf_val, nval)) == NULL) {
                         res = PyErr_NoMemory();
                         Py_DECREF(mylist);
-                        goto free_buf_list;
+                        goto freebufval;
+                    } else {
+                      buf_val = buf_val_tmp;
                     }
                     nalloc = nval;
                     continue;
-- 
2.39.5