Patchwork [2,of,7] parsers: don't leak references to sys et al in check_python_version

login
register
mail settings
Submitter Augie Fackler
Date Jan. 23, 2015, 9:06 p.m.
Message ID <5de7357d24e4e5444198.1422047199@arthedain.pit.corp.google.com>
Download mbox | patch
Permalink /patch/7541/
State Accepted
Commit 5fb44983a6963c9c3ad595346f80d03c76393d15
Headers show

Comments

Augie Fackler - Jan. 23, 2015, 9:06 p.m.
# HG changeset patch
# User Augie Fackler <augie@google.com>
# Date 1422045021 18000
#      Fri Jan 23 15:30:21 2015 -0500
# Branch stable
# Node ID 5de7357d24e4e5444198e82ab710dbffb9453d0a
# Parent  3435cf143b791612214a871337e8549e7d5c26a6
parsers: don't leak references to sys et al in check_python_version

Found with cpychecker.

Patch

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -2267,8 +2267,16 @@  static void module_init(PyObject *mod)
 
 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). */