From: Luca Weiss <[email protected]>

Fatal Python error: none_dealloc: deallocating None
Python runtime state: finalizing (tstate=0x000055c9bac70920)

Current thread 0x00007fbe34e47740 (most recent call first):
  <no Python frame>
Aborted (core dumped)

This is caused by a missing Py_INCREF on the returned Py_None, as
demonstrated e.g. in https://github.com/mythosil/swig-python-incref or
described at https://edcjones.tripod.com/refcount.html ("Remember to
INCREF Py_None!")

A PoC for triggering this crash is uploaded to
https://github.com/z3ntu/pylibfdt-crash .
With this patch applied to pylibfdt the crash does not happen.

This is a backport of dtc commit d152126bb029 ("Fix Python crash on
getprop deallocation").

Signed-off-by: Luca Weiss <[email protected]>
Reviewed-by: Simon Glass <[email protected]>
Signed-off-by: David Gibson <[email protected]>
[adapted to U-Boot]
Signed-off-by: Alexey Charkov <[email protected]>
---
 scripts/dtc/pylibfdt/libfdt.i_shipped | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/scripts/dtc/pylibfdt/libfdt.i_shipped 
b/scripts/dtc/pylibfdt/libfdt.i_shipped
index 55170431357b..5025893b1e95 100644
--- a/scripts/dtc/pylibfdt/libfdt.i_shipped
+++ b/scripts/dtc/pylibfdt/libfdt.i_shipped
@@ -1045,14 +1045,16 @@ typedef uint32_t fdt32_t;
 
 /* typemap used for fdt_getprop() */
 %typemap(out) (const void *) {
-       if (!$1)
+       if (!$1) {
                $result = Py_None;
-       else
+               Py_INCREF($result);
+       } else {
         %#if PY_VERSION_HEX >= 0x03000000
             $result = Py_BuildValue("y#", $1, (Py_ssize_t)*arg4);
         %#else
             $result = Py_BuildValue("s#", $1, (Py_ssize_t)*arg4);
         %#endif
+    }
 }
 
 /* typemap used for fdt_setprop() */

-- 
2.54.0

Reply via email to