Skip to content

Commit

Permalink
pythongh-110864: Fix _PyArg_UnpackKeywordsWithVararg overwriting vara…
Browse files Browse the repository at this point in the history
…rg with NULL (python#110868)
  • Loading branch information
sobolevn authored and aisk committed Feb 11, 2024
1 parent 0687b9f commit 6950753
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
35 changes: 35 additions & 0 deletions Lib/test/test_clinic.py
Expand Up @@ -3154,6 +3154,10 @@ def test_posonly_vararg(self):
self.assertEqual(ac_tester.posonly_vararg(1, 2), (1, 2, ()))
self.assertEqual(ac_tester.posonly_vararg(1, b=2), (1, 2, ()))
self.assertEqual(ac_tester.posonly_vararg(1, 2, 3, 4), (1, 2, (3, 4)))
with self.assertRaises(TypeError):
ac_tester.posonly_vararg(b=4)
with self.assertRaises(TypeError):
ac_tester.posonly_vararg(1, 2, 3, b=4)

def test_vararg_and_posonly(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -3204,6 +3208,37 @@ def test_gh_99240_double_free(self):
with self.assertRaisesRegex(TypeError, err):
ac_tester.gh_99240_double_free('a', '\0b')

def test_null_or_tuple_for_varargs(self):
# All of these should not crash:
valid_args_for_test = [
(('a',), {},
('a', (), False)),
(('a', 1, 2, 3), {'covariant': True},
('a', (1, 2, 3), True)),
((), {'name': 'a'},
('a', (), False)),
((), {'name': 'a', 'covariant': True},
('a', (), True)),
((), {'covariant': True, 'name': 'a'},
('a', (), True)),
]
for args, kwargs, expected in valid_args_for_test:
with self.subTest(args=args, kwargs=kwargs):
self.assertEqual(
ac_tester.null_or_tuple_for_varargs(*args, **kwargs),
expected,
)

def test_null_or_tuple_for_varargs_error(self):
with self.assertRaises(TypeError):
ac_tester.null_or_tuple_for_varargs(covariant=True)
with self.assertRaises(TypeError):
ac_tester.null_or_tuple_for_varargs(1, name='a')
with self.assertRaises(TypeError):
ac_tester.null_or_tuple_for_varargs(1, 2, 3, name='a', covariant=True)
with self.assertRaises(TypeError):
ac_tester.null_or_tuple_for_varargs(1, 2, 3, covariant=True, name='a')

def test_cloned_func_exception_message(self):
incorrect_arg = -1 # f1() and f2() accept a single str
with self.assertRaisesRegex(TypeError, "clone_f1"):
Expand Down
@@ -0,0 +1,2 @@
Fix argument parsing by ``_PyArg_UnpackKeywordsWithVararg`` for functions
defining pos-or-keyword, vararg, and kw-only parameters.
21 changes: 21 additions & 0 deletions Modules/_testclinic.c
Expand Up @@ -1128,6 +1128,26 @@ gh_99240_double_free_impl(PyObject *module, char *a, char *b)
Py_RETURN_NONE;
}

/*[clinic input]
null_or_tuple_for_varargs
name: object
*constraints: object
covariant: bool = False
See https://github.com/python/cpython/issues/110864
[clinic start generated code]*/

static PyObject *
null_or_tuple_for_varargs_impl(PyObject *module, PyObject *name,
PyObject *constraints, int covariant)
/*[clinic end generated code: output=a785b35421358983 input=c9bce186637956b3]*/
{
assert(name != NULL);
assert(constraints != NULL);
PyObject *c = covariant ? Py_True : Py_False;
return pack_arguments_newref(3, name, constraints, c);
}

/*[clinic input]
_testclinic.clone_f1 as clone_f1
Expand Down Expand Up @@ -1843,6 +1863,7 @@ static PyMethodDef tester_methods[] = {
GH_32092_KW_PASS_METHODDEF
GH_99233_REFCOUNT_METHODDEF
GH_99240_DOUBLE_FREE_METHODDEF
NULL_OR_TUPLE_FOR_VARARGS_METHODDEF
CLONE_F1_METHODDEF
CLONE_F2_METHODDEF
CLONE_WITH_CONV_F1_METHODDEF
Expand Down
72 changes: 71 additions & 1 deletion Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/getargs.c
Expand Up @@ -2522,7 +2522,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
*
* Otherwise, we leave a place at `buf[vararg]` for vararg tuple
* so the index is `i + 1`. */
if (nargs < vararg) {
if (nargs < vararg && i != vararg) {
buf[i] = current_arg;
}
else {
Expand Down

0 comments on commit 6950753

Please sign in to comment.