[Python-checkins] cpython (2.7): Issue #22453: Fexed reference leaks when format error messages in ceval.c.

serhiy.storchaka python-checkins at python.org
Tue Nov 18 23:17:27 CET 2014


https://hg.python.org/cpython/rev/6e26b5291c41
changeset:   93506:6e26b5291c41
branch:      2.7
parent:      93500:eb25629d2a46
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Wed Nov 19 00:11:05 2014 +0200
summary:
  Issue #22453: Fexed reference leaks when format error messages in ceval.c.
Warn against the use of leaking macro PyObject_REPR().

files:
  Include/object.h |   4 +++-
  Python/ceval.c   |  20 ++++++++++++++++----
  Python/compile.c |  12 ++++++------
  3 files changed, 25 insertions(+), 11 deletions(-)


diff --git a/Include/object.h b/Include/object.h
--- a/Include/object.h
+++ b/Include/object.h
@@ -527,7 +527,9 @@
 PyAPI_DATA(int) _Py_HashSecret_Initialized;
 #endif
 
-/* Helper for passing objects to printf and the like */
+/* Helper for passing objects to printf and the like.
+   Leaks refcounts.  Don't use it!
+*/
 #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
 
 /* Flag bits for printing: */
diff --git a/Python/ceval.c b/Python/ceval.c
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -1957,9 +1957,13 @@
                 if (err == 0) continue;
                 break;
             }
+            t = PyObject_Repr(w);
+            if (t == NULL)
+                break;
             PyErr_Format(PyExc_SystemError,
                          "no locals found when storing %s",
-                         PyObject_REPR(w));
+                         PyString_AS_STRING(t));
+            Py_DECREF(t);
             break;
 
         case DELETE_NAME:
@@ -1971,9 +1975,13 @@
                                          w);
                 break;
             }
+            t = PyObject_Repr(w);
+            if (t == NULL)
+                break;
             PyErr_Format(PyExc_SystemError,
                          "no locals when deleting %s",
-                         PyObject_REPR(w));
+                         PyString_AS_STRING(w));
+            Py_DECREF(t);
             break;
 
         PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
@@ -2046,10 +2054,14 @@
         case LOAD_NAME:
             w = GETITEM(names, oparg);
             if ((v = f->f_locals) == NULL) {
+                why = WHY_EXCEPTION;
+                t = PyObject_Repr(w);
+                if (t == NULL)
+                    break;
                 PyErr_Format(PyExc_SystemError,
                              "no locals when loading %s",
-                             PyObject_REPR(w));
-                why = WHY_EXCEPTION;
+                             PyString_AS_STRING(w));
+                Py_DECREF(t);
                 break;
             }
             if (PyDict_CheckExact(v)) {
diff --git a/Python/compile.c b/Python/compile.c
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -1271,11 +1271,11 @@
                       "symbols: %s\nlocals: %s\nglobals: %s",
                       PyString_AS_STRING(name),
                       PyString_AS_STRING(c->u->u_name),
-                      PyObject_REPR(c->u->u_ste->ste_id),
+                      PyString_AS_STRING(PyObject_Repr(c->u->u_ste->ste_id)),
                       c->c_filename,
-                      PyObject_REPR(c->u->u_ste->ste_symbols),
-                      PyObject_REPR(c->u->u_varnames),
-                      PyObject_REPR(c->u->u_names)
+                      PyString_AS_STRING(PyObject_Repr(c->u->u_ste->ste_symbols)),
+                      PyString_AS_STRING(PyObject_Repr(c->u->u_varnames)),
+                      PyString_AS_STRING(PyObject_Repr(c->u->u_names))
         );
         Py_FatalError(buf);
     }
@@ -1327,11 +1327,11 @@
         if (arg == -1) {
             printf("lookup %s in %s %d %d\n"
                 "freevars of %s: %s\n",
-                PyObject_REPR(name),
+                PyString_AS_STRING(PyObject_Repr(name)),
                 PyString_AS_STRING(c->u->u_name),
                 reftype, arg,
                 PyString_AS_STRING(co->co_name),
-                PyObject_REPR(co->co_freevars));
+                PyString_AS_STRING(PyObject_Repr(co->co_freevars)));
             Py_FatalError("compiler_make_closure()");
         }
         ADDOP_I(c, LOAD_CLOSURE, arg);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list