Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-124513: Check args in framelocalsproxy_new()
Fix a crash in FrameLocalsProxy constructor: check the number of
arguments.
  • Loading branch information
vstinner committed Sep 25, 2024
commit cdae0a44b194bf3b8ced0246e009c3b95246d5d0
17 changes: 17 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,23 @@ class ObjectSubclass:
with self.assertRaises(TypeError):
proxy[obj] = 0

def test_constructor(self):
FrameLocalsProxy = type([sys._getframe().f_locals
for x in range(1)][0])
self.assertEqual(FrameLocalsProxy.__name__, 'FrameLocalsProxy')

def make_frame():
x = 1
y = 2
return sys._getframe()

proxy = FrameLocalsProxy(make_frame())
self.assertEqual(proxy, {'x': 1, 'y': 2})

# constructor expects 1 argument (frame)
with self.assertRaises(TypeError):
FrameLocalsProxy()


class FrameLocalsProxyMappingTests(mapping_tests.TestHashMappingProtocol):
"""Test that FrameLocalsProxy behaves like a Mapping (with exceptions)"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in FrameLocalsProxy constructor: check the number of arguments.
Patch by Victor Stinner.
7 changes: 7 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ framelocalsproxy_dealloc(PyObject *self)
static PyObject *
framelocalsproxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Comment thread
vstinner marked this conversation as resolved.
{
if (PyTuple_GET_SIZE(args) != 1) {
PyErr_Format(PyExc_TypeError,
"FrameLocalsProxy expected 1 argument, got %zd",
PyTuple_GET_SIZE(args));
return NULL;
}

Comment thread
vstinner marked this conversation as resolved.
PyFrameLocalsProxyObject *self = (PyFrameLocalsProxyObject *)type->tp_alloc(type, 0);
if (self == NULL) {
return NULL;
Expand Down