From d171fc875a9b26bcfe73f06c8bc3f349ebe8997a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 18 Jul 2022 18:07:31 +0300 Subject: [PATCH 1/2] gh-94930: skipitem() in getargs.c should return non-NULL on error (GH-94931) (cherry picked from commit 067f0da33506f70c36a67d5f3d8d011c8dae10c9) Co-authored-by: Serhiy Storchaka --- Lib/test/test_getargs2.py | 16 ++++++++++--- ...2-07-17-18-21-40.gh-issue-94930.gPFGDL.rst | 2 ++ Modules/_testcapimodule.c | 23 +++++++++++++++++-- Python/getargs.c | 4 +--- 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/C API/2022-07-17-18-21-40.gh-issue-94930.gPFGDL.rst diff --git a/Lib/test/test_getargs2.py b/Lib/test/test_getargs2.py index 571cabb5f13a02a..fd1952c5845db73 100644 --- a/Lib/test/test_getargs2.py +++ b/Lib/test/test_getargs2.py @@ -878,9 +878,19 @@ def test_s_hash(self): def test_s_hash_int(self): # "s#" without PY_SSIZE_T_CLEAN defined. from _testcapi import getargs_s_hash_int - self.assertRaises(SystemError, getargs_s_hash_int, "abc") - self.assertRaises(SystemError, getargs_s_hash_int, x=42) - # getargs_s_hash_int() don't raise SystemError because skipitem() is not called. + from _testcapi import getargs_s_hash_int2 + buf = bytearray([1, 2]) + self.assertRaises(SystemError, getargs_s_hash_int, buf, "abc") + self.assertRaises(SystemError, getargs_s_hash_int, buf, x=42) + self.assertRaises(SystemError, getargs_s_hash_int, buf, x="abc") + self.assertRaises(SystemError, getargs_s_hash_int2, buf, ("abc",)) + self.assertRaises(SystemError, getargs_s_hash_int2, buf, x=42) + self.assertRaises(SystemError, getargs_s_hash_int2, buf, x="abc") + buf.append(3) # still mutable -- not locked by a buffer export + # getargs_s_hash_int(buf) may not raise SystemError because skipitem() + # is not called. But it is an implementation detail. + # getargs_s_hash_int(buf) + # getargs_s_hash_int2(buf) def test_z(self): from _testcapi import getargs_z diff --git a/Misc/NEWS.d/next/C API/2022-07-17-18-21-40.gh-issue-94930.gPFGDL.rst b/Misc/NEWS.d/next/C API/2022-07-17-18-21-40.gh-issue-94930.gPFGDL.rst new file mode 100644 index 000000000000000..5b79d757b1e4aa9 --- /dev/null +++ b/Misc/NEWS.d/next/C API/2022-07-17-18-21-40.gh-issue-94930.gPFGDL.rst @@ -0,0 +1,2 @@ +Fix ``SystemError`` raised when :c:func:`PyArg_ParseTupleAndKeywords` is +used with ``#`` in ``(...)`` but without ``PY_SSIZE_T_CLEAN`` defined. diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index aaf29ad01154410..e63b5b7e34933a4 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5619,6 +5619,7 @@ test_fatal_error(PyObject *self, PyObject *args) static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*); +static PyObject *getargs_s_hash_int2(PyObject *, PyObject *, PyObject*); static PyMethodDef TestMethods[] = { {"raise_exception", raise_exception, METH_VARARGS}, @@ -5730,6 +5731,8 @@ static PyMethodDef TestMethods[] = { {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, {"getargs_s_hash_int", (PyCFunction)(void(*)(void))getargs_s_hash_int, METH_VARARGS|METH_KEYWORDS}, + {"getargs_s_hash_int2", _PyCFunction_CAST(getargs_s_hash_int2), + METH_VARARGS|METH_KEYWORDS}, {"getargs_z", getargs_z, METH_VARARGS}, {"getargs_z_star", getargs_z_star, METH_VARARGS}, {"getargs_z_hash", getargs_z_hash, METH_VARARGS}, @@ -7419,11 +7422,27 @@ PyAPI_FUNC(int) PyArg_ParseTupleAndKeywords(PyObject *, PyObject *, static PyObject * getargs_s_hash_int(PyObject *self, PyObject *args, PyObject *kwargs) { - static char *keywords[] = {"", "x", NULL}; + static char *keywords[] = {"", "", "x", NULL}; + Py_buffer buf = {NULL}; + const char *s; + int len; + int i = 0; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "w*|s#i", keywords, &buf, &s, &len, &i)) + return NULL; + PyBuffer_Release(&buf); + Py_RETURN_NONE; +} + +static PyObject * +getargs_s_hash_int2(PyObject *self, PyObject *args, PyObject *kwargs) +{ + static char *keywords[] = {"", "", "x", NULL}; + Py_buffer buf = {NULL}; const char *s; int len; int i = 0; - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s#i", keywords, &s, &len, &i)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "w*|(s#)i", keywords, &buf, &s, &len, &i)) return NULL; + PyBuffer_Release(&buf); Py_RETURN_NONE; } diff --git a/Python/getargs.c b/Python/getargs.c index 92fc0aff322d26b..d92ea2d8e6b4631 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -2533,9 +2533,7 @@ skipitem(const char **p_format, va_list *p_va, int flags) if (*format == '#') { if (p_va != NULL) { if (!(flags & FLAG_SIZE_T)) { - PyErr_SetString(PyExc_SystemError, - "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); - return NULL; + return "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"; } (void) va_arg(*p_va, Py_ssize_t *); } From 5330564533d3e1f1d981fb5c39f5b36fc46df41e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 19 Jul 2022 14:13:27 +0300 Subject: [PATCH 2/2] Fix compilation error. --- Modules/_testcapimodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index e63b5b7e34933a4..56ab73b8ad05f77 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -5731,7 +5731,7 @@ static PyMethodDef TestMethods[] = { {"getargs_s_hash", getargs_s_hash, METH_VARARGS}, {"getargs_s_hash_int", (PyCFunction)(void(*)(void))getargs_s_hash_int, METH_VARARGS|METH_KEYWORDS}, - {"getargs_s_hash_int2", _PyCFunction_CAST(getargs_s_hash_int2), + {"getargs_s_hash_int2", (PyCFunction)(void(*)(void))getargs_s_hash_int2, METH_VARARGS|METH_KEYWORDS}, {"getargs_z", getargs_z, METH_VARARGS}, {"getargs_z_star", getargs_z_star, METH_VARARGS},