Commit a86c091a authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #22453: Fexed reference leaks when format error messages in ceval.c.

Warn against the use of leaking macro PyObject_REPR().
parent ead2f5a0
...@@ -527,7 +527,9 @@ PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret; ...@@ -527,7 +527,9 @@ PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret;
PyAPI_DATA(int) _Py_HashSecret_Initialized; PyAPI_DATA(int) _Py_HashSecret_Initialized;
#endif #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)) #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj))
/* Flag bits for printing: */ /* Flag bits for printing: */
......
...@@ -1957,9 +1957,13 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -1957,9 +1957,13 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
if (err == 0) continue; if (err == 0) continue;
break; break;
} }
t = PyObject_Repr(w);
if (t == NULL)
break;
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"no locals found when storing %s", "no locals found when storing %s",
PyObject_REPR(w)); PyString_AS_STRING(t));
Py_DECREF(t);
break; break;
case DELETE_NAME: case DELETE_NAME:
...@@ -1971,9 +1975,13 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -1971,9 +1975,13 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
w); w);
break; break;
} }
t = PyObject_Repr(w);
if (t == NULL)
break;
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"no locals when deleting %s", "no locals when deleting %s",
PyObject_REPR(w)); PyString_AS_STRING(w));
Py_DECREF(t);
break; break;
PREDICTED_WITH_ARG(UNPACK_SEQUENCE); PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
...@@ -2046,10 +2054,14 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -2046,10 +2054,14 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
case LOAD_NAME: case LOAD_NAME:
w = GETITEM(names, oparg); w = GETITEM(names, oparg);
if ((v = f->f_locals) == NULL) { if ((v = f->f_locals) == NULL) {
why = WHY_EXCEPTION;
t = PyObject_Repr(w);
if (t == NULL)
break;
PyErr_Format(PyExc_SystemError, PyErr_Format(PyExc_SystemError,
"no locals when loading %s", "no locals when loading %s",
PyObject_REPR(w)); PyString_AS_STRING(w));
why = WHY_EXCEPTION; Py_DECREF(t);
break; break;
} }
if (PyDict_CheckExact(v)) { if (PyDict_CheckExact(v)) {
......
...@@ -1271,11 +1271,11 @@ get_ref_type(struct compiler *c, PyObject *name) ...@@ -1271,11 +1271,11 @@ get_ref_type(struct compiler *c, PyObject *name)
"symbols: %s\nlocals: %s\nglobals: %s", "symbols: %s\nlocals: %s\nglobals: %s",
PyString_AS_STRING(name), PyString_AS_STRING(name),
PyString_AS_STRING(c->u->u_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, c->c_filename,
PyObject_REPR(c->u->u_ste->ste_symbols), PyString_AS_STRING(PyObject_Repr(c->u->u_ste->ste_symbols)),
PyObject_REPR(c->u->u_varnames), PyString_AS_STRING(PyObject_Repr(c->u->u_varnames)),
PyObject_REPR(c->u->u_names) PyString_AS_STRING(PyObject_Repr(c->u->u_names))
); );
Py_FatalError(buf); Py_FatalError(buf);
} }
...@@ -1327,11 +1327,11 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) ...@@ -1327,11 +1327,11 @@ compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
if (arg == -1) { if (arg == -1) {
printf("lookup %s in %s %d %d\n" printf("lookup %s in %s %d %d\n"
"freevars of %s: %s\n", "freevars of %s: %s\n",
PyObject_REPR(name), PyString_AS_STRING(PyObject_Repr(name)),
PyString_AS_STRING(c->u->u_name), PyString_AS_STRING(c->u->u_name),
reftype, arg, reftype, arg,
PyString_AS_STRING(co->co_name), PyString_AS_STRING(co->co_name),
PyObject_REPR(co->co_freevars)); PyString_AS_STRING(PyObject_Repr(co->co_freevars)));
Py_FatalError("compiler_make_closure()"); Py_FatalError("compiler_make_closure()");
} }
ADDOP_I(c, LOAD_CLOSURE, arg); ADDOP_I(c, LOAD_CLOSURE, arg);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment