[Libguestfs] [libnbd PATCH 1/5] python: Avoid memleak on (unlikely) module failure

Richard W.M. Jones rjones at redhat.com
Sat Jun 4 10:04:47 UTC 2022


On Fri, Jun 03, 2022 at 05:26:31PM -0500, Eric Blake wrote:
> Python 3.10 added PyModule_AddObjectRef() to more easily avoid a
> common memory leak when ading to a module fails (unlikely in our case,
> since we initialize early in the python process, but still something
> we must worry about for corner-case correctness).  But since we target
> older Python, we must check for errors and clean up ourselves.
> 
> Fixes: 259d46cb ("python: Raise a custome exception containing error string and errno.", v0.1.6)
> ---
>  generator/Python.ml | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/generator/Python.ml b/generator/Python.ml
> index 1c4446e..3f672ba 100644
> --- a/generator/Python.ml
> +++ b/generator/Python.ml
> @@ -143,9 +143,11 @@ let
>      return NULL;
> 
>    nbd_internal_py_Error = PyErr_NewException (\"nbd.Error\", NULL, NULL);
> -  if (nbd_internal_py_Error == NULL)
> +  if (PyModule_AddObject (mod, \"Error\", nbd_internal_py_Error) < 0) {
> +    Py_XDECREF (nbd_internal_py_Error);
> +    Py_DECREF (mod);
>      return NULL;
> -  PyModule_AddObject (mod, \"Error\", nbd_internal_py_Error);
> +  }
> 

Reviewed-by: Richard W.M. Jones <rjones at redhat.com>

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into KVM guests.
http://libguestfs.org/virt-v2v


More information about the Libguestfs mailing list