parsers: don't leak references to sys et al in check_python_version stable
authorAugie Fackler <augie@google.com>
Fri, 23 Jan 2015 15:30:21 -0500
branchstable
changeset 23943 5fb44983a696
parent 23942 fefa5f2a1730
child 23944 ec28f8b66e62
parsers: don't leak references to sys et al in check_python_version Found with cpychecker.
mercurial/parsers.c
--- a/mercurial/parsers.c	Fri Jan 23 15:19:04 2015 -0500
+++ b/mercurial/parsers.c	Fri Jan 23 15:30:21 2015 -0500
@@ -2267,8 +2267,16 @@
 
 static int check_python_version(void)
 {
-	PyObject *sys = PyImport_ImportModule("sys");
-	long hexversion = PyInt_AsLong(PyObject_GetAttrString(sys, "hexversion"));
+	PyObject *sys = PyImport_ImportModule("sys"), *ver;
+	long hexversion;
+	if (!sys)
+		return -1;
+	ver = PyObject_GetAttrString(sys, "hexversion");
+	Py_DECREF(sys);
+	if (!ver)
+		return -1;
+	hexversion = PyInt_AsLong(ver);
+	Py_DECREF(ver);
 	/* sys.hexversion is a 32-bit number by default, so the -1 case
 	 * should only occur in unusual circumstances (e.g. if sys.hexversion
 	 * is manually set to an invalid value). */