Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] GH-105840: Fix assertion failures when specializing calls with too many __defaults__ (GH-105847) #105864

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,35 @@ def f():
self.assertFalse(f())


class TestCallCache(unittest.TestCase):
def test_too_many_defaults_0(self):
def f():
pass

f.__defaults__ = (None,)
for _ in range(1025):
f()

def test_too_many_defaults_1(self):
def f(x):
pass

f.__defaults__ = (None, None)
for _ in range(1025):
f(None)
f()

def test_too_many_defaults_2(self):
def f(x, y):
pass

f.__defaults__ = (None, None, None)
for _ in range(1025):
f(None, None)
f(None)
f()


if __name__ == "__main__":
import unittest
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix possible crashes when specializing function calls with too many
``__defaults__``.
4 changes: 2 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1500,9 +1500,9 @@ specialize_py_call(PyFunctionObject *func, _Py_CODEUNIT *instr, int nargs,
}
int argcount = code->co_argcount;
int defcount = func->func_defaults == NULL ? 0 : (int)PyTuple_GET_SIZE(func->func_defaults);
assert(defcount <= argcount);
int min_args = argcount-defcount;
if (nargs > argcount || nargs < min_args) {
// GH-105840: min_args is negative when somebody sets too many __defaults__!
if (min_args < 0 || nargs > argcount || nargs < min_args) {
SPECIALIZATION_FAIL(CALL, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
return -1;
}
Expand Down