[Lldb-commits] [lldb] 4a38d33 - [lldb] Fix double free in python bindings error handling.

Jorge Gorbe Moya via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 27 16:07:47 PDT 2023


Author: Jorge Gorbe Moya
Date: 2023-03-27T16:07:36-07:00
New Revision: 4a38d33268959309dd97d9ef423327607bda4104

URL: https://github.com/llvm/llvm-project/commit/4a38d33268959309dd97d9ef423327607bda4104
DIFF: https://github.com/llvm/llvm-project/commit/4a38d33268959309dd97d9ef423327607bda4104.diff

LOG: [lldb] Fix double free in python bindings error handling.

If we have a `%typemap(freearg)` that frees the argument, we shouldn't
free it manually on an error path before calling `SWIG_fail`.
`SWIG_fail` will already free the memory in this case, and doing it
manually results in a double free.

Differential Revision: https://reviews.llvm.org/D147007

Added: 
    

Modified: 
    lldb/bindings/python/python-typemaps.swig

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/python/python-typemaps.swig b/lldb/bindings/python/python-typemaps.swig
index 3e9675c8c00f1..d2520c415e741 100644
--- a/lldb/bindings/python/python-typemaps.swig
+++ b/lldb/bindings/python/python-typemaps.swig
@@ -1,4 +1,10 @@
-/* Typemap definitions, to allow SWIG to properly handle 'char**' data types. */
+/* Typemap definitions, to allow SWIG to properly handle 'char**' data types.
+
+NOTE: If there's logic to free memory in a %typemap(freearg), it will also be
+run if you call SWIG_fail on an error path. Don't manually free() an argument
+AND call SWIG_fail at the same time, because it will result in a double free.
+
+*/
 
 %inline %{
 
@@ -17,7 +23,6 @@
       PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>();
       if (!py_str.IsAllocated()) {
         PyErr_SetString(PyExc_TypeError, "list must contain strings");
-        free($1);
         SWIG_fail;
       }
 
@@ -294,12 +299,10 @@ template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
       PyObject *o = PyList_GetItem($input, i);
       if (!SetNumberFromPyObject($1[i], o)) {
         PyErr_SetString(PyExc_TypeError, "list must contain numbers");
-        free($1);
         SWIG_fail;
       }
 
       if (PyErr_Occurred()) {
-        free($1);
         SWIG_fail;
       }
     }


        


More information about the lldb-commits mailing list