# HG changeset patch
# User ZyX <[email protected]>
# Date 1369489639 -14400
# Node ID d85c9583a4c924cfc80b2816c4fc3605a592e746
# Parent  4070da40d25e86084109ed015fe943d75413d612
Fix some possible memory leaks

diff -r 4070da40d25e -r d85c9583a4c9 src/if_py_both.h
--- a/src/if_py_both.h  Sat May 25 18:04:33 2013 +0400
+++ b/src/if_py_both.h  Sat May 25 17:47:19 2013 +0400
@@ -32,9 +32,17 @@
 
 #define DICTKEY_DECL \
     PyObject   *dictkey_todecref;
-#define DICTKEY_GET(err) \
+#define DICTKEY_GET(err, decref) \
     if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
+    { \
+       if (decref) \
+       { \
+           Py_DECREF(keyObject); \
+       } \
        return err; \
+    } \
+    if (decref && !dictkey_todecref) \
+       dictkey_todecref = keyObject; \
     if (*key == NUL) \
     { \
        PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
@@ -602,7 +610,6 @@
                    Py_DECREF(newObj);
                    return NULL;
                }
-               Py_DECREF(newObj);
            }
        }
     }
@@ -947,7 +954,7 @@
     dictitem_T *di;
     DICTKEY_DECL
 
-    DICTKEY_GET(NULL)
+    DICTKEY_GET(NULL, 0)
 
     di = dict_find(self->dict, key, -1);
 
@@ -977,7 +984,7 @@
        return -1;
     }
 
-    DICTKEY_GET(-1)
+    DICTKEY_GET(-1, 0)
 
     di = dict_find(dict, key, -1);
 
@@ -1650,7 +1657,7 @@
     if (self->Check(self->from))
        return NULL;
 
-    DICTKEY_GET(NULL)
+    DICTKEY_GET(NULL, 0)
 
     flags = get_option_value_strict(key, &numval, &stringval,
                                    self->opt_type, self->from);
@@ -1789,7 +1796,7 @@
     if (self->Check(self->from))
        return -1;
 
-    DICTKEY_GET(-1)
+    DICTKEY_GET(-1, 0)
 
     flags = get_option_value_strict(key, NULL, NULL,
                                    self->opt_type, self->from);
@@ -4028,12 +4035,10 @@
     {
        DICTKEY_DECL
 
-       if (keyObject == NULL)
+       if (keyObject == NULL || valObject == NULL)
            return -1;
-       if (valObject == NULL)
-           return -1;
-
-       DICTKEY_GET(-1)
+
+       DICTKEY_GET(-1, 0)
 
        di = dictitem_alloc(key);
 
@@ -4054,6 +4059,7 @@
 
        if (dict_add(dict, di) == FAIL)
        {
+           clear_tv(&di->di_tv);
            vim_free(di);
            PyErr_SetVim(_("failed to add key to dictionary"));
            return -1;
@@ -4099,24 +4105,25 @@
            return -1;
        }
 
-       keyObject = PyTuple_GetItem(litem, 0);
-       if (keyObject == NULL)
+       if (!(keyObject = PyTuple_GetItem(litem, 0)))
        {
            Py_DECREF(list);
            Py_DECREF(litem);
            return -1;
        }
 
-       DICTKEY_GET(-1)
-
-       valObject = PyTuple_GetItem(litem, 1);
-       if (valObject == NULL)
+       DICTKEY_GET(-1, 1)
+
+       if (!(valObject = PyTuple_GetItem(litem, 1)))
        {
            Py_DECREF(list);
            Py_DECREF(litem);
+           DICTKEY_UNREF
            return -1;
        }
 
+       Py_DECREF(litem);
+
        di = dictitem_alloc(key);
 
        DICTKEY_UNREF
@@ -4124,7 +4131,7 @@
        if (di == NULL)
        {
            Py_DECREF(list);
-           Py_DECREF(litem);
+           Py_DECREF(valObject);
            PyErr_NoMemory();
            return -1;
        }
@@ -4134,18 +4141,20 @@
        {
            vim_free(di);
            Py_DECREF(list);
-           Py_DECREF(litem);
+           Py_DECREF(valObject);
            return -1;
        }
+
+       Py_DECREF(valObject);
+
        if (dict_add(dict, di) == FAIL)
        {
+           clear_tv(&di->di_tv);
            vim_free(di);
            Py_DECREF(list);
-           Py_DECREF(litem);
            PyErr_SetVim(_("failed to add key to dictionary"));
            return -1;
        }
-       Py_DECREF(litem);
     }
     Py_DECREF(list);
     return 0;
@@ -4200,13 +4209,18 @@
        li = listitem_alloc();
        if (li == NULL)
        {
+           Py_DECREF(iterator);
            PyErr_NoMemory();
            return -1;
        }
        li->li_tv.v_lock = 0;
 
        if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
+       {
+           Py_DECREF(item);
+           Py_DECREF(iterator);
            return -1;
+       }
 
        list_append(l, li);
 
@@ -4240,8 +4254,12 @@
 # else
        capsule = PyCObject_FromVoidPtr(tv, NULL);
 # endif
-       PyDict_SetItemString(lookup_dict, hexBuf, capsule);
-       Py_DECREF(capsule);
+       if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
+       {
+           Py_DECREF(capsule);
+           tv->v_type = VAR_UNKNOWN;
+           return -1;
+       }
        if (py_to_tv(obj, tv, lookup_dict) == -1)
        {
            tv->v_type = VAR_UNKNOWN;

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


*** /tmp/extdiff.CL1K17/vim.4070da40d25e/src/if_py_both.h	2013-05-25 18:40:14.909321368 +0400
--- vim.d85c9583a4c9/src/if_py_both.h	2013-05-25 18:40:14.915318313 +0400
***************
*** 32,40 ****
  
  #define DICTKEY_DECL \
      PyObject	*dictkey_todecref;
! #define DICTKEY_GET(err) \
      if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
  	return err; \
      if (*key == NUL) \
      { \
  	PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
--- 32,48 ----
  
  #define DICTKEY_DECL \
      PyObject	*dictkey_todecref;
! #define DICTKEY_GET(err, decref) \
      if (!(key = StringToChars(keyObject, &dictkey_todecref))) \
+     { \
+ 	if (decref) \
+ 	{ \
+ 	    Py_DECREF(keyObject); \
+ 	} \
  	return err; \
+     } \
+     if (decref && !dictkey_todecref) \
+ 	dictkey_todecref = keyObject; \
      if (*key == NUL) \
      { \
  	PyErr_SetString(PyExc_ValueError, _("empty keys are not allowed")); \
***************
*** 602,608 ****
  		    Py_DECREF(newObj);
  		    return NULL;
  		}
- 		Py_DECREF(newObj);
  	    }
  	}
      }
--- 610,615 ----
***************
*** 947,953 ****
      dictitem_T	*di;
      DICTKEY_DECL
  
!     DICTKEY_GET(NULL)
  
      di = dict_find(self->dict, key, -1);
  
--- 954,960 ----
      dictitem_T	*di;
      DICTKEY_DECL
  
!     DICTKEY_GET(NULL, 0)
  
      di = dict_find(self->dict, key, -1);
  
***************
*** 977,983 ****
  	return -1;
      }
  
!     DICTKEY_GET(-1)
  
      di = dict_find(dict, key, -1);
  
--- 984,990 ----
  	return -1;
      }
  
!     DICTKEY_GET(-1, 0)
  
      di = dict_find(dict, key, -1);
  
***************
*** 1650,1656 ****
      if (self->Check(self->from))
  	return NULL;
  
!     DICTKEY_GET(NULL)
  
      flags = get_option_value_strict(key, &numval, &stringval,
  				    self->opt_type, self->from);
--- 1657,1663 ----
      if (self->Check(self->from))
  	return NULL;
  
!     DICTKEY_GET(NULL, 0)
  
      flags = get_option_value_strict(key, &numval, &stringval,
  				    self->opt_type, self->from);
***************
*** 1789,1795 ****
      if (self->Check(self->from))
  	return -1;
  
!     DICTKEY_GET(-1)
  
      flags = get_option_value_strict(key, NULL, NULL,
  				    self->opt_type, self->from);
--- 1796,1802 ----
      if (self->Check(self->from))
  	return -1;
  
!     DICTKEY_GET(-1, 0)
  
      flags = get_option_value_strict(key, NULL, NULL,
  				    self->opt_type, self->from);
***************
*** 4028,4039 ****
      {
  	DICTKEY_DECL
  
! 	if (keyObject == NULL)
! 	    return -1;
! 	if (valObject == NULL)
  	    return -1;
  
! 	DICTKEY_GET(-1)
  
  	di = dictitem_alloc(key);
  
--- 4035,4044 ----
      {
  	DICTKEY_DECL
  
! 	if (keyObject == NULL || valObject == NULL)
  	    return -1;
  
! 	DICTKEY_GET(-1, 0)
  
  	di = dictitem_alloc(key);
  
***************
*** 4054,4059 ****
--- 4059,4065 ----
  
  	if (dict_add(dict, di) == FAIL)
  	{
+ 	    clear_tv(&di->di_tv);
  	    vim_free(di);
  	    PyErr_SetVim(_("failed to add key to dictionary"));
  	    return -1;
***************
*** 4099,4122 ****
  	    return -1;
  	}
  
! 	keyObject = PyTuple_GetItem(litem, 0);
! 	if (keyObject == NULL)
  	{
  	    Py_DECREF(list);
  	    Py_DECREF(litem);
  	    return -1;
  	}
  
! 	DICTKEY_GET(-1)
  
! 	valObject = PyTuple_GetItem(litem, 1);
! 	if (valObject == NULL)
  	{
  	    Py_DECREF(list);
  	    Py_DECREF(litem);
  	    return -1;
  	}
  
  	di = dictitem_alloc(key);
  
  	DICTKEY_UNREF
--- 4105,4129 ----
  	    return -1;
  	}
  
! 	if (!(keyObject = PyTuple_GetItem(litem, 0)))
  	{
  	    Py_DECREF(list);
  	    Py_DECREF(litem);
  	    return -1;
  	}
  
! 	DICTKEY_GET(-1, 1)
  
! 	if (!(valObject = PyTuple_GetItem(litem, 1)))
  	{
  	    Py_DECREF(list);
  	    Py_DECREF(litem);
+ 	    DICTKEY_UNREF
  	    return -1;
  	}
  
+ 	Py_DECREF(litem);
+ 
  	di = dictitem_alloc(key);
  
  	DICTKEY_UNREF
***************
*** 4124,4130 ****
  	if (di == NULL)
  	{
  	    Py_DECREF(list);
! 	    Py_DECREF(litem);
  	    PyErr_NoMemory();
  	    return -1;
  	}
--- 4131,4137 ----
  	if (di == NULL)
  	{
  	    Py_DECREF(list);
! 	    Py_DECREF(valObject);
  	    PyErr_NoMemory();
  	    return -1;
  	}
***************
*** 4134,4151 ****
  	{
  	    vim_free(di);
  	    Py_DECREF(list);
! 	    Py_DECREF(litem);
  	    return -1;
  	}
  	if (dict_add(dict, di) == FAIL)
  	{
  	    vim_free(di);
  	    Py_DECREF(list);
- 	    Py_DECREF(litem);
  	    PyErr_SetVim(_("failed to add key to dictionary"));
  	    return -1;
  	}
- 	Py_DECREF(litem);
      }
      Py_DECREF(list);
      return 0;
--- 4141,4160 ----
  	{
  	    vim_free(di);
  	    Py_DECREF(list);
! 	    Py_DECREF(valObject);
  	    return -1;
  	}
+ 
+ 	Py_DECREF(valObject);
+ 
  	if (dict_add(dict, di) == FAIL)
  	{
+ 	    clear_tv(&di->di_tv);
  	    vim_free(di);
  	    Py_DECREF(list);
  	    PyErr_SetVim(_("failed to add key to dictionary"));
  	    return -1;
  	}
      }
      Py_DECREF(list);
      return 0;
***************
*** 4200,4212 ****
--- 4209,4226 ----
  	li = listitem_alloc();
  	if (li == NULL)
  	{
+ 	    Py_DECREF(iterator);
  	    PyErr_NoMemory();
  	    return -1;
  	}
  	li->li_tv.v_lock = 0;
  
  	if (_ConvertFromPyObject(item, &li->li_tv, lookup_dict) == -1)
+ 	{
+ 	    Py_DECREF(item);
+ 	    Py_DECREF(iterator);
  	    return -1;
+ 	}
  
  	list_append(l, li);
  
***************
*** 4240,4247 ****
  # else
  	capsule = PyCObject_FromVoidPtr(tv, NULL);
  # endif
! 	PyDict_SetItemString(lookup_dict, hexBuf, capsule);
! 	Py_DECREF(capsule);
  	if (py_to_tv(obj, tv, lookup_dict) == -1)
  	{
  	    tv->v_type = VAR_UNKNOWN;
--- 4254,4265 ----
  # else
  	capsule = PyCObject_FromVoidPtr(tv, NULL);
  # endif
! 	if (PyDict_SetItemString(lookup_dict, hexBuf, capsule))
! 	{
! 	    Py_DECREF(capsule);
! 	    tv->v_type = VAR_UNKNOWN;
! 	    return -1;
! 	}
  	if (py_to_tv(obj, tv, lookup_dict) == -1)
  	{
  	    tv->v_type = VAR_UNKNOWN;

Raspunde prin e-mail lui