From 6f1bdfd3f340fcb9488fe68a167a48d24607496d Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 26 Jan 2024 18:55:14 +0000 Subject: [PATCH] python: Pakfire.execute(): Fix parsing some arguments There have been a couple of problems here when no or invalid values have been passed. Signed-off-by: Michael Tremer --- src/_pakfire/pakfire.c | 50 +++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 16b745a7..a5a45e54 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -500,7 +500,7 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* if (!PyUnicode_Check(item)) { PyErr_Format(PyExc_TypeError, "Item %u in command is not a string", i); - return NULL; + goto ERROR; } // Copy to argv @@ -526,12 +526,6 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* goto ERROR; } - // Check callback - if (callback && !PyCallable_Check(callback)) { - PyErr_SetString(PyExc_TypeError, "callback must be callable\n"); - goto ERROR; - } - // Set nice if (nice) { r = pakfire_jail_nice(jail, nice); @@ -569,32 +563,34 @@ static PyObject* Pakfire_execute(PakfireObject* self, PyObject* args, PyObject* } } - const Py_ssize_t num_bind = PySequence_Length(bind); - // Bind - for (unsigned int i = 0; i < num_bind; i++) { - PyObject* b = PySequence_ITEM(bind, i); - if (!b) - goto ERROR; + if (bind && PySequence_Check(bind)) { + const Py_ssize_t num_bind = PySequence_Length(bind); - // Check if this is a Unicode object - if (!PyUnicode_Check(b)) { - PyErr_SetString(PyExc_ValueError, "bind contains a non-Unicode object"); - Py_DECREF(b); - goto ERROR; - } + for (unsigned int i = 0; i < num_bind; i++) { + PyObject* b = PySequence_ITEM(bind, i); + if (!b) + goto ERROR; - const char* path = PyUnicode_AsUTF8(b); + // Check if this is a Unicode object + if (!PyUnicode_Check(b)) { + PyErr_SetString(PyExc_ValueError, "bind contains a non-Unicode object"); + Py_DECREF(b); + goto ERROR; + } + + const char* path = PyUnicode_AsUTF8(b); + + // Perform bind + r = pakfire_jail_bind(jail, path, path, 0); + if (r) { + PyErr_SetFromErrno(PyExc_OSError); + Py_DECREF(b); + goto ERROR; + } - // Perform bind - r = pakfire_jail_bind(jail, path, path, 0); - if (r) { - PyErr_SetFromErrno(PyExc_OSError); Py_DECREF(b); - goto ERROR; } - - Py_DECREF(b); } Py_BEGIN_ALLOW_THREADS -- 2.39.2