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

login
register
mail settings
Submitter Augie Fackler
Date Jan. 28, 2015, 5:50 p.m.
Message ID <425737cc5cde5b990a60.1422467419@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/7572/
State Accepted
Commit 2d2c0a8eeeb8764daa94400e7de16a02971361a9
Headers show

Comments

Augie Fackler - Jan. 28, 2015, 5:50 p.m.
# HG changeset patch
# User Augie Fackler <augie@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.
Matt Mackall - Jan. 28, 2015, 8:38 p.m.
On Wed, 2015-01-28 at 12:50 -0500, Augie Fackler wrote:
> # HG changeset patch
> # User Augie Fackler <augie@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

Queued for stable, thanks.

Patch

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);