Patchwork [1,of,6,cpychecker] parsers: fix two leaks in index_ancestors

login
register
mail settings
Submitter Augie Fackler
Date Aug. 18, 2015, 9:54 p.m.
Message ID <f8fda0f125089c010496.1439934851@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/10234/
State Accepted
Headers show

Comments

Augie Fackler - Aug. 18, 2015, 9:54 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1439932504 14400
#      Tue Aug 18 17:15:04 2015 -0400
# Node ID f8fda0f125089c01049651d26fcd955200a02663
# Parent  b3ad349d0e50c613cfb26b3d61f08dc573dd5087
parsers: fix two leaks in index_ancestors

Both happy paths through this function leaked the returned list:

1) If the list was of size 0 or 1, it was retained an extra time and then
   returned.

2) If the list was passed to find_deepest, it was never released before
   exiting this function.

Both paths spotted by cpychecker.

Patch

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -2158,16 +2158,18 @@  bail:
  */
 static PyObject *index_ancestors(indexObject *self, PyObject *args)
 {
+	PyObject *ret;
 	PyObject *gca = index_commonancestorsheads(self, args);
 	if (gca == NULL)
 		return NULL;
 
 	if (PyList_GET_SIZE(gca) <= 1) {
-		Py_INCREF(gca);
 		return gca;
 	}
 
-	return find_deepest(self, gca);
+	ret = find_deepest(self, gca);
+	Py_DECREF(gca);
+	return ret;
 }
 
 /*