[PATCH stable cpychecker v2] osutil: fix memory leak of PyBytes of path in statfiles

Augie Fackler raf at durin42.com
Wed Jan 28 17:50:19 UTC 2015


# HG changeset patch
# User Augie Fackler <augie at google.com>
# Date 1422371836 18000
#      Tue Jan 27 10:17:16 2015 -0500
# Branch stable
# Node ID 425737cc5cde5b990a6052955cbc5d19ff898675
# Parent  8f02682ff3b03a86abc09ff5b0397842ea70ba07
osutil: fix memory leak of PyBytes of path in statfiles

Spotted with cpychecker.

diff --git a/mercurial/osutil.c b/mercurial/osutil.c
--- a/mercurial/osutil.c
+++ b/mercurial/osutil.c
@@ -410,17 +410,22 @@ static PyObject *statfiles(PyObject *sel
 		return NULL;
 
 	for (i = 0; i < count; i++) {
-		PyObject *stat;
+		PyObject *stat, *pypath;
 		struct stat st;
 		int ret, kind;
 		char *path;
 
-		path = PyString_AsString(PySequence_GetItem(names, i));
+		pypath = PySequence_GetItem(names, i);
+		if (!pypath)
+			return NULL;
+		path = PyString_AsString(pypath);
 		if (path == NULL) {
+			Py_DECREF(pypath);
 			PyErr_SetString(PyExc_TypeError, "not a string");
 			goto bail;
 		}
 		ret = lstat(path, &st);
+		Py_DECREF(pypath);
 		kind = st.st_mode & S_IFMT;
 		if (ret != -1 && (kind == S_IFREG || kind == S_IFLNK)) {
 			stat = makestat(&st);


More information about the Mercurial-devel mailing list