10000 [3.12] GH-105840: Fix assertion failures when specializing calls with… · python/cpython@560adb0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 560adb0

Browse files
[3.12] GH-105840: Fix assertion failures when specializing calls with too many __defaults__ (GH-105863)
GH-105840: Fix assertion failures when specializing calls with too many __defaults__ (GH-105847) (cherry picked from commit 2beab5b) Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
1 parent 32c0aeb commit 560adb0

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
@@ -1666,9 +1666,9 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
16661666
}
16671667
int argcount = code->co_argcount;
16681668
int defcount = func->func_defaults == NULL ? 0 : (int)PyTuple_GET_SIZE(func->func_defaults);
1669-
assert(defcount <= argcount);
16701669
int min_args = argcount-defcount;
1671-
if (nargs > argcount || nargs < min_args) {
1670+
// GH-105840: min_args is negative when somebody sets too many __defaults__!
1671+
if (min_args < 0 || nargs > argcount || nargs < min_args) {
16721672
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
16731673
return -1;
16741674
}

0 commit comments

Comments
 (0)
0