Skip to content

Commit c2192a2

Browse files
authored
gh-110864: Fix _PyArg_UnpackKeywordsWithVararg overwriting vararg with NULL (#110868)
1 parent db656ae commit c2192a2

File tree

5 files changed

+130
-2
lines changed

5 files changed

+130
-2
lines changed

Lib/test/test_clinic.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3154,6 +3154,10 @@ def test_posonly_vararg(self):
31543154
self.assertEqual(ac_tester.posonly_vararg(1, 2), (1, 2, ()))
31553155
self.assertEqual(ac_tester.posonly_vararg(1, b=2), (1, 2, ()))
31563156
self.assertEqual(ac_tester.posonly_vararg(1, 2, 3, 4), (1, 2, (3, 4)))
3157+
with self.assertRaises(TypeError):
3158+
ac_tester.posonly_vararg(b=4)
3159+
with self.assertRaises(TypeError):
3160+
ac_tester.posonly_vararg(1, 2, 3, b=4)
31573161

31583162
def test_vararg_and_posonly(self):
31593163
with self.assertRaises(TypeError):
@@ -3204,6 +3208,37 @@ def test_gh_99240_double_free(self):
32043208
with self.assertRaisesRegex(TypeError, err):
32053209
ac_tester.gh_99240_double_free('a', '\0b')
32063210

3211+
def test_null_or_tuple_for_varargs(self):
3212+
# All of these should not crash:
3213+
valid_args_for_test = [
3214+
(('a',), {},
3215+
('a', (), False)),
3216+
(('a', 1, 2, 3), {'covariant': True},
3217+
('a', (1, 2, 3), True)),
3218+
((), {'name': 'a'},
3219+
('a', (), False)),
3220+
((), {'name': 'a', 'covariant': True},
3221+
('a', (), True)),
3222+
((), {'covariant': True, 'name': 'a'},
3223+
('a', (), True)),
3224+
]
3225+
for args, kwargs, expected in valid_args_for_test:
3226+
with self.subTest(args=args, kwargs=kwargs):
3227+
self.assertEqual(
3228+
ac_tester.null_or_tuple_for_varargs(*args, **kwargs),
3229+
expected,
3230+
)
3231+
3232+
def test_null_or_tuple_for_varargs_error(self):
3233+
with self.assertRaises(TypeError):
3234+
ac_tester.null_or_tuple_for_varargs(covariant=True)
3235+
with self.assertRaises(TypeError):
3236+
ac_tester.null_or_tuple_for_varargs(1, name='a')
3237+
with self.assertRaises(TypeError):
3238+
ac_tester.null_or_tuple_for_varargs(1, 2, 3, name='a', covariant=True)
3239+
with self.assertRaises(TypeError):
3240+
ac_tester.null_or_tuple_for_varargs(1, 2, 3, covariant=True, name='a')
3241+
32073242
def test_cloned_func_exception_message(self):
32083243
incorrect_arg = -1 # f1() and f2() accept a single str
32093244
with self.assertRaisesRegex(TypeError, "clone_f1"):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix argument parsing by ``_PyArg_UnpackKeywordsWithVararg`` for functions
2+
defining pos-or-keyword, vararg, and kw-only parameters.

Modules/_testclinic.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,26 @@ gh_99240_double_free_impl(PyObject *module, char *a, char *b)
11281128
Py_RETURN_NONE;
11291129
}
11301130

1131+
/*[clinic input]
1132+
null_or_tuple_for_varargs
1133+
1134+
name: object
1135+
*constraints: object
1136+
covariant: bool = False
1137+
1138+
See https://github.com/python/cpython/issues/110864
1139+
[clinic start generated code]*/
1140+
1141+
static PyObject *
1142+
null_or_tuple_for_varargs_impl(PyObject *module, PyObject *name,
1143+
PyObject *constraints, int covariant)
1144+
/*[clinic end generated code: output=a785b35421358983 input=c9bce186637956b3]*/
1145+
{
1146+
assert(name != NULL);
1147+
assert(constraints != NULL);
1148+
PyObject *c = covariant ? Py_True : Py_False;
1149+
return pack_arguments_newref(3, name, constraints, c);
1150+
}
11311151

11321152
/*[clinic input]
11331153
_testclinic.clone_f1 as clone_f1
@@ -1843,6 +1863,7 @@ static PyMethodDef tester_methods[] = {
18431863
GH_32092_KW_PASS_METHODDEF
18441864
GH_99233_REFCOUNT_METHODDEF
18451865
GH_99240_DOUBLE_FREE_METHODDEF
1866+
NULL_OR_TUPLE_FOR_VARARGS_METHODDEF
18461867
CLONE_F1_METHODDEF
18471868
CLONE_F2_METHODDEF
18481869
CLONE_WITH_CONV_F1_METHODDEF

Modules/clinic/_testclinic.c.h

Lines changed: 71 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/getargs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
25222522
*
25232523
* Otherwise, we leave a place at `buf[vararg]` for vararg tuple
25242524
* so the index is `i + 1`. */
2525-
if (nargs < vararg) {
2525+
if (nargs < vararg && i != vararg) {
25262526
buf[i] = current_arg;
25272527
}
25282528
else {

0 commit comments

Comments
 (0)