From 22673d1adfba1d304d44ad6c3fe3aacdd39611d1 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 1/3] 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 28254d7f11b409bc255099f55c489f83b570f115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 16:41:37 +0000 Subject: [PATCH 2/3] Strengthen adapter attribute error handling Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- P4API.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/P4API.cpp b/P4API.cpp index 657fd09..539a4ab 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -712,6 +712,12 @@ static PyObject * P4Adapter_getattro(P4Adapter *self, PyObject * nameObject) static int P4Adapter_setattro(P4Adapter *self, PyObject * nameObject, PyObject * value) { const char * name = GetPythonString(nameObject); + if (!name) + return -1; + + // deletion is not supported + if (!value) + return PyErr_SetString(PyExc_AttributeError, "deletion not supported"), -1; // Special case first: // If there is a specific ObjectSetter for this name available use this one @@ -752,7 +758,10 @@ static int P4Adapter_setattro(P4Adapter *self, PyObject * nameObject, PyObject * if (IsString(value)) { PythonClientAPI::strsetter ssetter = self->clientAPI->GetStrSetter(name); if (ssetter) { - return (self->clientAPI->*ssetter)(GetPythonString(value)); + const char *cstr = GetPythonString(value); + if (!cstr) + return -1; + return (self->clientAPI->*ssetter)(cstr); } else { ostringstream os; @@ -765,7 +774,14 @@ static int P4Adapter_setattro(P4Adapter *self, PyObject * nameObject, PyObject * // can only set int and string or certain object values -> bail out with exception ostringstream os; - os << "Cannot set attribute : " << name << " with value " << GetPythonString(PyObject_Str(value)); + PyObject *str = PyObject_Str(value); + const char *cstr = str ? GetPythonString(str) : 0; + if (!str || !cstr) { + Py_XDECREF(str); + return -1; + } + os << "Cannot set attribute : " << name << " with value " << cstr; + Py_DECREF(str); PyErr_SetString(PyExc_AttributeError, os.str().c_str()); return -1; From 418aa0662eb0b86c619145aeb0eaeb5653785978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 18:07:03 +0000 Subject: [PATCH 3/3] Guard integer attributes and charset conversion sizes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- P4API.cpp | 19 ++++++++++++++----- PythonClientAPI.cpp | 13 ++++++++++++- p4test.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/P4API.cpp b/P4API.cpp index 539a4ab..06e9c7e 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -123,9 +123,14 @@ P4Adapter_init(P4Adapter *self, PyObject *args, PyObject *kwds) if (PyInt_Check(value)) { PythonClientAPI::intsetter isetter = self->clientAPI->GetIntSetter(name); if (isetter) { - int result = (self->clientAPI->*isetter)(PyInt_AS_LONG(value)); - if (result) - return result; + long lval = PyInt_AsLong(value); + if (lval == -1 && PyErr_Occurred()) + return -1; + if (lval < INT_MIN || lval > INT_MAX) + return PyErr_SetString(PyExc_OverflowError, "integer out of range"), -1; + int result = (self->clientAPI->*isetter)((int)lval); + if (result) + return result; } else { ostringstream os; @@ -730,7 +735,12 @@ static int P4Adapter_setattro(P4Adapter *self, PyObject * nameObject, PyObject * if (PyInt_Check(value)) { PythonClientAPI::intsetter isetter = self->clientAPI->GetIntSetter(name); if (isetter) { - return (self->clientAPI->*isetter)(PyInt_AS_LONG(value)); + long lval = PyInt_AsLong(value); + if (lval == -1 && PyErr_Occurred()) + return -1; + if (lval < INT_MIN || lval > INT_MAX) + return PyErr_SetString(PyExc_OverflowError, "integer attribute value out of range"), -1; + return (self->clientAPI->*isetter)((int)lval); } else { ostringstream os; @@ -1541,4 +1551,3 @@ initP4API(void) return module; #endif } - diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 1e4e517..5f9e0a0 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -620,10 +620,21 @@ PyObject * PythonClientAPI::Convert(const char * charset, PyObject * content) } PyObject * bytes = PyUnicode_AsUTF8String(content); + if (!bytes) { + delete cvt; + return NULL; + } + Py_ssize_t slen = PyBytes_GET_SIZE(bytes); + if (slen > INT_MAX) { + PyErr_SetString(PyExc_OverflowError, "content overflow"); + Py_DECREF(bytes); + delete cvt; + return NULL; + } const char * contentAsUTF8 = PyBytes_AS_STRING(bytes); int retlen = 0; - const char * converted = cvt->FastCvt(contentAsUTF8, (int)strlen(contentAsUTF8), &retlen); + const char * converted = cvt->FastCvt(contentAsUTF8, (int)slen, &retlen); Py_DECREF( bytes ); // we do not need this object anymore if (converted == NULL) { diff --git a/p4test.py b/p4test.py index 16acaf1..b6513fa 100644 --- a/p4test.py +++ b/p4test.py @@ -973,6 +973,42 @@ def testArguments( self ): self.assertEqual(p4.port, "9999") self.assertEqual(p4.client, "myclient") + def testAdapterAttributeErrorsAndBounds(self): + import ctypes + + bits = ctypes.sizeof(ctypes.c_int) * 8 + minimum, maximum = -(1 << (bits - 1)), (1 << (bits - 1)) - 1 + for value in (minimum, maximum): + self.p4.maxresults = value + self.assertEqual(self.p4.maxresults, value) + self.assertEqual(P4API.P4Adapter(maxresults=value).maxresults, value) + for value in (minimum - 1, maximum + 1, -(1 << 100), 1 << 100): + with self.assertRaises(OverflowError): + self.p4.maxresults = value + with self.assertRaises(OverflowError): + P4API.P4Adapter(maxresults=value) + + with self.assertRaises(AttributeError): + del self.p4.input + with self.assertRaises(UnicodeEncodeError): + setattr(self.p4, '\ud800', 1) + with self.assertRaises(UnicodeEncodeError): + self.p4.port = '\ud800' + + class BadString: + def __str__(self): + raise ValueError("cannot stringify") + + with self.assertRaisesRegex(ValueError, "cannot stringify"): + self.p4.port = BadString() + + def testConvertContentLength(self): + adapter = P4API.P4Adapter() + convert = getattr(adapter, '__convert') + self.assertEqual(convert('iso8859-1', 'a\0b'), b'a\0b') + with self.assertRaises(UnicodeEncodeError): + convert('iso8859-1', '\ud800') + def testUnicode( self ): self.enableUnicode()