Visitar URL original
gh-99110: Initialize frame->previous in init_frame to fix segmentation fault by byllyfish · Pull Request #100182 · python/cpython · GitHub
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
Initialize frame->previous in _PyFrame_InitializeSpecials.
  • Loading branch information
byllyfish committed Dec 12, 2022
commit b2e72562707b5724917f59ffae6a5dd3931a086d
1 change: 1 addition & 0 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ _PyFrame_InitializeSpecials(
frame->f_locals = locals;
frame->stacktop = code->co_nlocalsplus;
frame->frame_obj = NULL;
frame->previous = NULL;
frame->prev_instr = _PyCode_CODE(code) - 1;
frame->yield_offset = 0;
frame->owner = FRAME_OWNED_BY_THREAD;
Expand Down
31 changes: 31 additions & 0 deletions Lib/test/test_ctypes/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,36 @@ def test_pyobject_repr(self):
self.assertEqual(repr(py_object(42)), "py_object(42)")
self.assertEqual(repr(py_object(object)), "py_object(%r)" % object)

def test_PyFrame_New_f_back(self):
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated
"""Test that accessing `f_back` does not cause a segmentation fault on
a frame created with ctypes (GH-99110)."""
# Adapted from:
# https://naleraphael.github.io/blog/posts/devlog_create_a_builtin_frame_object/

p_memtype = POINTER(c_ulong if sizeof(c_void_p) == 8 else c_uint)
pythonapi.PyFrame_New.argtypes = (
p_memtype, # PyThreadState *tstate
p_memtype, # PyCodeObject *code
py_object, # PyObject *globals
py_object # PyObject *locals
)
pythonapi.PyFrame_New.restype = py_object # PyFrameObject*
pythonapi.PyThreadState_Get.argtypes = None
pythonapi.PyThreadState_Get.restype = p_memtype

def dummy():
pass

frame = pythonapi.PyFrame_New(
pythonapi.PyThreadState_Get(), # thread state
cast(id(dummy.__code__), p_memtype), # a code object
globals(),
locals(),
)

# The following line should not cause a segmentation fault.
assert frame.f_back is None
Comment thread
kumaraditya303 marked this conversation as resolved.
Outdated


if __name__ == "__main__":
unittest.main()