[issue1782] PyModule_AddIntConstant and PyModule_AddStringConstant can leak
Hrvoje Nikšić
report at bugs.python.org
Thu Jan 10 15:47:52 CET 2008
New submission from Hrvoje Nikšić:
PyModule_AddObject has somewhat strange reference-counting behavior in
that it *conditionally* steals a reference. In case of error it doesn't
change the reference to the passed object, but in case of success it
steals it. This means that, as written, PyModule_AddIntConstant and
PyModuleAddStringConstant can leak created objects if PyModule_AddObject
fails.
As far as I can tell, the correct way to write those functions would be
(using PyModule_AddIntConstant as the example):
int
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
{
PyObject *o = PyInt_FromLong(value);
if (PyModule_AddObject(m, name, o) == 0)
return 0;
Py_XDECREF(o);
return -1;
}
PyModule_AddObject was obviously intended to enable writing the "simple"
code (it even gracefully handles being passed NULL object to add) like
the one in PyModule_AddIntConstant, but I don't see a way to enable such
usage and avoid both leaks and an interface change. Changing the
reference-counting behavior of PyModule_AddObject would be
backward-incompatible, but it might be a good idea to consider it for
Python 3.
If there is agreement that my analysis and the proposed fixes are
correct, I will produce a proper patch.
----------
components: Interpreter Core
messages: 59662
nosy: hniksic
severity: normal
status: open
title: PyModule_AddIntConstant and PyModule_AddStringConstant can leak
type: resource usage
versions: Python 2.5
__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue1782>
__________________________________
More information about the Python-bugs-list
mailing list