From 4d836e77a4a0923151a52f0dedc2c9dcde857444 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 10:34:48 +0000 Subject: [PATCH 1/4] clean up P4.run() argument error handling and reference counting --- P4API.cpp | 46 +++++++++++++++++++++++++++++++++++----------- p4test.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/P4API.cpp b/P4API.cpp index 657fd09..7c03150 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -223,35 +223,60 @@ static PyObject * P4Adapter_set_var(P4Adapter * self, PyObject *args) static PyObject * P4Adapter_run(P4Adapter * self, PyObject * args) { - PyObject * cmd = PyTuple_GetItem(args, 0); - if (cmd == NULL) { - return NULL; - } - + // assume the args are flattened already vector argv; - for (Py_ssize_t i = 1; i < PyTuple_Size(args); ++i) { + vector converted; + for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i) { PyObject * item = PyTuple_GET_ITEM(args, i); + bool did_convert = false; #if PY_MAJOR_VERSION >= 3 - // check encoding here? if( ! PyUnicode_Check(item) && ! PyBytes_Check(item) ) { // preserve plain Unicode and Byte strings item = PyObject_Str(item); + did_convert = true; } #else if( ! PyBytes_Check(item) ) { item = PyObject_Str(item); + did_convert = true; } #endif - argv.push_back(GetPythonString(item)); + if (item == NULL) { + for (PyObject * obj : converted) { + Py_DECREF(obj); + } + return NULL; + } + if (did_convert) { + converted.push_back(item); + } + const char * cstr = GetPythonString(item); + if (!cstr) { + for (PyObject * obj : converted) { + Py_DECREF(obj); + } + return NULL; + } + argv.push_back(cstr); } + + if (argv.empty()) { + PyErr_SetString(PyExc_RuntimeError, "No command provided"); + return NULL; + } + const char * cmd = argv[0]; // this is a bit of a hack: it assumes the storage layout of the vector is continuous // the other hack is that the API expects (char * const *), but this cannot be stored // a std::vector<>, because it cannot exchange pointers - return self->clientAPI->Run(GetPythonString(cmd), (int)argv.size(), - (argv.size() > 0) ? (char * const *) &argv[0] : NULL ); + PyObject * result = self->clientAPI->Run(cmd, (int)argv.size()-1, + (argv.size() > 1) ? (char * const *) &argv[1] : NULL ); + for (PyObject * obj : converted) { + Py_DECREF(obj); + } + return result; } static PyObject * P4API_identify(PyObject * self) @@ -1525,4 +1550,3 @@ initP4API(void) return module; #endif } - diff --git a/p4test.py b/p4test.py index 16acaf1..da27766 100644 --- a/p4test.py +++ b/p4test.py @@ -2787,6 +2787,43 @@ def testEncodingModes(self): changes = self.p4.run('changes', b'-m1') self.assertIsInstance(changes, list, "bytes argument under utf-8 encoding was not handled") + info_bytes = self.p4.run(b'info') + self.assertIsInstance(info_bytes, list, + "bytes command under utf-8 encoding was not handled") + + def testRunArgumentConversionErrors(self): + """Check that run() propagates conversion errors and releases converted args.""" + class Stringifiable: + def __str__(self): + return shared_string + + class RaisesOnString: + def __str__(self): + raise ValueError("argument conversion failed") + + shared_string = "converted argument" + original_refcount = sys.getrefcount(shared_string) + + with self.assertRaises(ValueError) as context: + self.p4.run('info', Stringifiable(), RaisesOnString()) + self.assertEqual(str(context.exception), "argument conversion failed") + self.assertEqual(sys.getrefcount(shared_string), original_refcount, + "Converted argument was not released after a later conversion failed") + + with self.assertRaises(UnicodeEncodeError): + self.p4.run('info', Stringifiable(), '\ud800') + self.assertEqual(sys.getrefcount(shared_string), original_refcount, + "Converted argument was not released after UTF-8 conversion failed") + + # The Python wrapper must also surface errors while encoding str arguments. + self.p4.encoding = 'ascii' + with self.assertRaises(UnicodeEncodeError): + self.p4.run('info', '\u2603') + + # An empty argument list must fail cleanly before attempting to execute a command. + with self.assertRaises(RuntimeError) as context: + self.p4.run() + self.assertEqual(str(context.exception), "No command provided") def testLogMessages(self): """Cover the run() logger integration: info logging on success and the From 503634d7435eff8579e056e60761d7876f4220c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 11:59:25 +0000 Subject: [PATCH 2/4] GetPythonString returns NULL and sets exception if not a str or bytes --- SpecMgr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpecMgr.cpp b/SpecMgr.cpp index 19dcf7f..bd07514 100644 --- a/SpecMgr.cpp +++ b/SpecMgr.cpp @@ -769,7 +769,7 @@ const char * GetPythonString(PyObject *obj) { return PyUnicode_AsUTF8(obj); } else - return PyBytes_AS_STRING(obj); + return PyBytes_AsString(obj); } #endif From 6531355ceff96e70c4212cbbe517f6f737db63e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 18:05:00 +0000 Subject: [PATCH 3/4] Guard P4.run argument count against overflow Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- P4API.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/P4API.cpp b/P4API.cpp index 7c03150..0daf680 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -229,7 +229,7 @@ static PyObject * P4Adapter_run(P4Adapter * self, PyObject * args) vector argv; vector converted; for (Py_ssize_t i = 0; i < PyTuple_Size(args); ++i) { - PyObject * item = PyTuple_GET_ITEM(args, i); + PyObject * item = PyTuple_GET_ITEM(args, i); bool did_convert = false; #if PY_MAJOR_VERSION >= 3 if( ! PyUnicode_Check(item) && ! PyBytes_Check(item) ) { // preserve plain Unicode and Byte strings @@ -271,8 +271,14 @@ static PyObject * P4Adapter_run(P4Adapter * self, PyObject * args) // the other hack is that the API expects (char * const *), but this cannot be stored // a std::vector<>, because it cannot exchange pointers - PyObject * result = self->clientAPI->Run(cmd, (int)argv.size()-1, - (argv.size() > 1) ? (char * const *) &argv[1] : NULL ); + size_t nargs = argv.size()-1; + PyObject *result = NULL; + if (nargs > INT_MAX) { + PyErr_SetString(PyExc_OverflowError, "too many arguments"); + } else { + result = self->clientAPI->Run(cmd, (int)nargs, + (argv.size() > 1) ? (char * const *) &argv[1] : NULL ); + } for (PyObject * obj : converted) { Py_DECREF(obj); } From 3be61f3f2380c99132fc3cb6f4673293c510eb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 18:18:17 +0000 Subject: [PATCH 4/4] Remove trailing whitespace near argument limit Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- P4API.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/P4API.cpp b/P4API.cpp index 0daf680..a75462c 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -270,7 +270,7 @@ static PyObject * P4Adapter_run(P4Adapter * self, PyObject * args) // this is a bit of a hack: it assumes the storage layout of the vector is continuous // the other hack is that the API expects (char * const *), but this cannot be stored // a std::vector<>, because it cannot exchange pointers - + size_t nargs = argv.size()-1; PyObject *result = NULL; if (nargs > INT_MAX) {