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

Skip to content

Commit 2944a6c

Browse files
[3.11] GH-105840: Fix assertion failures when specializing calls with too many __defaults__ (GH-105864)
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 7c877b5 commit 2944a6c

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
@@ -429,6 +429,35 @@ def f():
429429
self.assertFalse(f())
430430

431431

432+
class TestCallCache(unittest.TestCase):
433+
def test_too_many_defaults_0(self):
434+
def f():
435+
pass
436+
437+
f.__defaults__ = (None,)
438+
for _ in range(1025):
439+
f()
440+
441+
def test_too_many_defaults_1(self):
442+
def f(x):
443+
pass
444+
445+
f.__defaults__ = (None, None)
446+
for _ in range(1025):
447+
f(None)
448+
f()
449+
450+
def test_too_many_defaults_2(self):
451+
def f(x, y):
452+
pass
453+
454+
f.__defaults__ = (None, None, None)
455+
for _ in range(1025):
456+
f(None, None)
457+
f(None)
458+
f()
459+
460+
432461
if __name__ == "__main__":
433462
import unittest
434463
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
@@ -1500,9 +1500,9 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
15001500
}
15011501
int argcount = code->co_argcount;
15021502
int defcount = func->func_defaults == NULL ? 0 : (int)PyTuple_GET_SIZE(func->func_defaults);
1503-
assert(defcount <= argcount);
15041503
int min_args = argcount-defcount;
1505-
if (nargs > argcount || nargs < min_args) {
1504+
// GH-105840: min_args is negative when somebody sets too many __defaults__!
1505+
if (min_args < 0 || nargs > argcount || nargs < min_args) {
15061506
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
15071507
return -1;
15081508
}

0 commit comments

Comments
 (0)
0