Skip to content

Commit a7b8a96

Browse files
[3.8] bpo-38913: Fix segfault in Py_BuildValue("(sGH-O)", ...) if entered with exception raised. (GH-18656). (GH-18732)
(cherry picked from commit 28d0bca)
1 parent 5f2ade2 commit a7b8a96

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed segfault in ``Py_BuildValue()`` called with a format containing "#"
2+
and undefined PY_SSIZE_T_CLEAN whwn an exception is set.

Modules/_testcapimodule.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5063,6 +5063,8 @@ test_write_unraisable_exc(PyObject *self, PyObject *args)
50635063
}
50645064

50655065

5066+
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
5067+
50665068
static PyMethodDef TestMethods[] = {
50675069
{"raise_exception", raise_exception, METH_VARARGS},
50685070
{"raise_memoryerror", raise_memoryerror, METH_NOARGS},
@@ -5122,6 +5124,7 @@ static PyMethodDef TestMethods[] = {
51225124
#endif
51235125
{"getbuffer_with_null_view", getbuffer_with_null_view, METH_O},
51245126
{"test_buildvalue_N", test_buildvalue_N, METH_NOARGS},
5127+
{"test_buildvalue_issue38913", test_buildvalue_issue38913, METH_NOARGS},
51255128
{"get_args", get_args, METH_VARARGS},
51265129
{"get_kwargs", (PyCFunction)(void(*)(void))get_kwargs, METH_VARARGS|METH_KEYWORDS},
51275130
{"getargs_tuple", getargs_tuple, METH_VARARGS},
@@ -6332,3 +6335,42 @@ PyInit__testcapi(void)
63326335
PyState_AddModule(m, &_testcapimodule);
63336336
return m;
63346337
}
6338+
6339+
6340+
/* Test the C API exposed when PY_SSIZE_T_CLEAN is not defined */
6341+
6342+
#undef Py_BuildValue
6343+
PyAPI_FUNC(PyObject *) Py_BuildValue(const char *, ...);
6344+
6345+
static PyObject *
6346+
test_buildvalue_issue38913(PyObject *self, PyObject *Py_UNUSED(ignored))
6347+
{
6348+
PyObject *res;
6349+
const char str[] = "string";
6350+
const Py_UNICODE unicode[] = L"unicode";
6351+
PyErr_SetNone(PyExc_ZeroDivisionError);
6352+
6353+
res = Py_BuildValue("(s#O)", str, 1, Py_None);
6354+
assert(res == NULL);
6355+
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6356+
return NULL;
6357+
}
6358+
res = Py_BuildValue("(z#O)", str, 1, Py_None);
6359+
assert(res == NULL);
6360+
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6361+
return NULL;
6362+
}
6363+
res = Py_BuildValue("(y#O)", str, 1, Py_None);
6364+
assert(res == NULL);
6365+
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6366+
return NULL;
6367+
}
6368+
res = Py_BuildValue("(u#O)", unicode, 1, Py_None);
6369+
assert(res == NULL);
6370+
if (!PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
6371+
return NULL;
6372+
}
6373+
6374+
PyErr_Clear();
6375+
Py_RETURN_NONE;
6376+
}

Python/modsupport.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
343343
if (flags & FLAG_SIZE_T)
344344
n = va_arg(*p_va, Py_ssize_t);
345345
else {
346+
n = va_arg(*p_va, int);
346347
if (PyErr_WarnEx(PyExc_DeprecationWarning,
347348
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
348349
return NULL;
349350
}
350-
n = va_arg(*p_va, int);
351351
}
352352
}
353353
else
@@ -396,11 +396,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
396396
if (flags & FLAG_SIZE_T)
397397
n = va_arg(*p_va, Py_ssize_t);
398398
else {
399+
n = va_arg(*p_va, int);
399400
if (PyErr_WarnEx(PyExc_DeprecationWarning,
400401
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
401402
return NULL;
402403
}
403-
n = va_arg(*p_va, int);
404404
}
405405
}
406406
else
@@ -434,11 +434,11 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
434434
if (flags & FLAG_SIZE_T)
435435
n = va_arg(*p_va, Py_ssize_t);
436436
else {
437+
n = va_arg(*p_va, int);
437438
if (PyErr_WarnEx(PyExc_DeprecationWarning,
438439
"PY_SSIZE_T_CLEAN will be required for '#' formats", 1)) {
439440
return NULL;
440441
}
441-
n = va_arg(*p_va, int);
442442
}
443443
}
444444
else

0 commit comments

Comments
 (0)