8000 GH-105840: Fix assertion failures when specializing calls with too ma… · python/cpython@2beab5b · GitHub
[go: up one dir, main page]

Skip to content

Commit 2beab5b

Browse files
authored
GH-105840: Fix assertion failures when specializing calls with too many __defaults__ (GH-105847)
1 parent b356a47 commit 2beab5b

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Lib/test/test_opcache.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,35 @@ def f():
452452
self.assertFalse(f())
453453

454454

455+
class TestCallCache(unittest.TestCase):
456+
def test_too_many_defaults_0(self):
457+
def f():
458+
pass
459+
460+
f.__defaults__ = (None,)
461+
for _ in range(1025):
462+
f()
463+
464+
def test_too_many_defaults_1(self):
465+
def f(x):
466+
pass
467+
468+
f.__defaults__ = (None, None)
469+
for _ in range(1025):
470+
f(None)
471+
f()
472+
473+
def test_too_many_defaults_2(self):
474+
def f(x, y):
475+
pass
476+
477+
f.__defaults__ = (None, None, None)
478+
for _ in range(1025):
479+
f(None, None)
480+
f(None)
481+
f()
482+
483+
455484
if __name__ == "__main__":
456485
import unittest
457486
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix possible crashes when specializing function calls with too many
2+
``__defaults__``.

Python/specialize.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,9 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
16471647
}
16481648
int argcount = code->co_argcount;
16491649
int defcount = func->func_defaults == NULL ? 0 : (int)PyTuple_GET_SIZE(func->func_defaults);
1650-
assert(defcount <= argcount);
16511650
int min_args = argcount-defcount;
1652-
if (nargs > argcount || nargs < min_args) {
1651+
// GH-105840: min_args is negative when somebody sets too many __defaults__!
1652+
if (min_args < 0 || nargs > argcount || nargs < min_args) {
16531653
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
16541654
return -1;
16551655
}

0 commit comments

Comments
 (0)
0