changeset 23943:5fb44983a696 stable

parsers: don't leak references to sys et al in check_python_version Found with cpychecker.
author Augie Fackler <augie@google.com>
date Fri, 23 Jan 2015 15:30:21 -0500
parents fefa5f2a1730
children ec28f8b66e62
files mercurial/parsers.c
diffstat 1 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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). */