From 4375f5b91e440e53c1d311fed36f61ec0b54d71f Mon Sep 17 00:00:00 2001 From: Jean-Michel Picod Date: Tue, 20 Dec 2022 15:32:38 +0100 Subject: [PATCH] Avoid memory leaks with Python <3.10 --- client/src/cmdscript.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index 9e19c0959..7a5a59130 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -426,8 +426,6 @@ static int CmdScriptRun(const char *Cmd) { } PySys_SetArgv(argc + 1, py_args); - - // clean up #else // The following line will implicitly pre-initialize Python PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); @@ -453,6 +451,12 @@ static int CmdScriptRun(const char *Cmd) { return PM3_ESOFT; } int ret = Pm3PyRun_SimpleFileNoExit(f, filename); +#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 10 + // Py_DecodeLocale() allocates memory that needs to be free'd + for (int i = 0; i < argc + 1; i++) { + PyMem_RawFree(py_args[i]); + } +#endif Py_Finalize(); free(script_path); if (ret) {