[Python-Dev] Coverity Scan

Antoine Pitrou solipsis at pitrou.net
Fri Jul 26 16:48:03 CEST 2013


Le Fri, 26 Jul 2013 16:29:59 +0200,
Christian Heimes <christian at python.org> a écrit :
> Coverity is able to detect some cases of refcount leaks. I don't know
> if the software is able to keep track of all reference counts. But it
> understands missing Py_DECREF() in error branches.
> 
> For example:
> 
> PyObject *n = PyLong_FromLong(0);
> PyObject *u = PyUnicode_FromString("example");
> 
> if (u == NULL) {
>     return NULL;
>     /* Coverity detects that 'n' leaks memory */
> }

But 'n' doesn't leak memory since PyLong_FromLong(0) is statically
allocated ;-)

More generally, in similar cases (e.g. replace "0" with a non-small
integer), you don't need any knowledge of reference counts to infer
that there is a memory leak. When the code discards the only existing
pointer to a heap-allocated memory area, there's a leak.

What we call "refcount leaks" is generally when an area is still
pointer-accessible, but failure to decrement the reference count
appropriately means it will never be released.

Regards

Antoine.




More information about the Python-Dev mailing list