Skip to content

[3.8] bpo-38913: Fix segfault in Py_BuildValue("(sGH-O)", ...) if entered with exception raised. (GH-18656). #18732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed segfault in ``Py_BuildValue()`` called with a format containing "#"
and undefined PY_SSIZE_T_CLEAN whwn an exception is set.
42 changes: 42 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5063,6 +5063,8 @@ test_write_unraisable_exc(PyObject *self, PyObject *args)
}


static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);

static PyMethodDef TestMethods[] = {
{"raise_exception", raise_exception, METH_VARARGS},
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
Expand Down Expand Up @@ -5122,6 +5124,7 @@ static PyMethodDef TestMethods[] = {
#endif
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
{"get_args", get_args, METH_VARARGS},
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS},
{"getargs_tuple", getargs_tuple, METH_VARARGS},
Expand Down Expand Up @@ -6332,3 +6335,42 @@ PyInit__testcapi(void)
PyState_AddModule(m, &_testcapimodule);
return m;
}


/* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */

#undef Py_BuildValue
PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...);

static PyObject *
test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *res;
const char str[] = "string";
const Py_UNICODE unicode[] = L"unicode";
PyErr_SetNone(PyExc_ZeroDivisionError);

res = Py_BuildValue("(s#O)", str, 1, Py_None);
assert(res == NULL);
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
return NULL;
}
res = Py_BuildValue("(z#O)", str, 1, Py_None);
assert(res == NULL);
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
return NULL;
}
res = Py_BuildValue("(y#O)", str, 1, Py_None);
assert(res == NULL);
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
return NULL;
}
res = Py_BuildValue("(u#O)", unicode, 1, Py_None);
assert(res == NULL);
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
return NULL;
}

PyErr_Clear();
Py_RETURN_NONE;
}
6 changes: 3 additions & 3 deletions Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
if (flags & FLAG_SIZE_T)
n = va_arg(*p_va, Py_ssize_t);
else {
n = va_arg(*p_va, int);
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
return NULL;
}
n = va_arg(*p_va, int);
}
}
else
Expand Down Expand Up @@ -396,11 +396,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
if (flags & FLAG_SIZE_T)
n = va_arg(*p_va, Py_ssize_t);
else {
n = va_arg(*p_va, int);
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
return NULL;
}
n = va_arg(*p_va, int);
}
}
else
Expand Down Expand Up @@ -434,11 +434,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
if (flags & FLAG_SIZE_T)
n = va_arg(*p_va, Py_ssize_t);
else {
n = va_arg(*p_va, int);
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
return NULL;
}
n = va_arg(*p_va, int);
}
}
else
Expand Down