From 96f85d38bcdd31631bd7c0a884d7f1a551ae57e4 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 11 Nov 2024 21:28:17 +0100 Subject: [PATCH] some PyStatus --- client/src/cmdscript.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/client/src/cmdscript.c b/client/src/cmdscript.c index 004da456f..d25063b33 100644 --- a/client/src/cmdscript.c +++ b/client/src/cmdscript.c @@ -440,6 +440,7 @@ static int CmdScriptRun(const char *Cmd) { Py_Initialize(); #else PyConfig py_conf; + PyStatus status; // We need to use Python mode instead of isolated to avoid breaking stuff. PyConfig_InitPythonConfig(&py_conf); // Let's still make things bit safer by being as close as possible to isolated mode. @@ -465,8 +466,10 @@ static int CmdScriptRun(const char *Cmd) { PySys_SetArgv(argc + 1, py_args); #else // The following line will implicitly pre-initialize Python - PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); - + status = PyConfig_SetBytesArgv(&py_conf, argc + 1, argv); + if (PyStatus_Exception(status)) { + goto pyexception; + } // We disallowed in py_conf environment variables interfering with python interpreter's behavior. // Let's manually enable the ones we truly need. const char *virtual_env = getenv("VIRTUAL_ENV"); @@ -474,15 +477,27 @@ static int CmdScriptRun(const char *Cmd) { size_t length = strlen(virtual_env) + strlen("/bin/python3") + 1; char python_executable_path[length]; snprintf(python_executable_path, length, "%s/bin/python3", virtual_env); - PyConfig_SetBytesString(&py_conf, &py_conf.executable, python_executable_path); + status = PyConfig_SetBytesString(&py_conf, &py_conf.executable, python_executable_path); + if (PyStatus_Exception(status)) { + goto pyexception; + } } else { // This is required by Proxspace to work with an isolated Python configuration - PyConfig_SetBytesString(&py_conf, &py_conf.home, getenv("PYTHONHOME")); + status = PyConfig_SetBytesString(&py_conf, &py_conf.home, getenv("PYTHONHOME")); + if (PyStatus_Exception(status)) { + goto pyexception; + } } // This is required for allowing `import pm3` in python scripts - PyConfig_SetBytesString(&py_conf, &py_conf.pythonpath_env, getenv("PYTHONPATH")); + status = PyConfig_SetBytesString(&py_conf, &py_conf.pythonpath_env, getenv("PYTHONPATH")); + if (PyStatus_Exception(status)) { + goto pyexception; + } - Py_InitializeFromConfig(&py_conf); + status = Py_InitializeFromConfig(&py_conf); + if (PyStatus_Exception(status)) { + goto pyexception; + } // clean up PyConfig_Clear(&py_conf); @@ -516,6 +531,16 @@ static int CmdScriptRun(const char *Cmd) { PrintAndLogEx(SUCCESS, "\nfinished " _YELLOW_("%s"), filename); return PM3_SUCCESS; } + +pyexception: + PyConfig_Clear(&py_conf); + if (PyStatus_IsExit(status)) { + PrintAndLogEx(WARNING, "\nPython initialization failed with exitcode=%i", status.exitcode); + } + if (PyStatus_IsError(status)) { + PrintAndLogEx(WARNING, "\nPython initialization failed with exception: %s", status.err_msg); + } + return PM3_ESOFT; } #endif