Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-99110: Initialize frame->previous in init_frame to fix segmentation fault#100182

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kumaraditya303 merged 10 commits intopython:mainfrombyllyfish:fix-issue-99110
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from1 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
PrevPrevious commit
NextNext commit
Add frame_new function to _testcapimodule.c.
Move frame test from test_python_api.py to test_frame.py.
  • Loading branch information
@byllyfish
byllyfish committedDec 18, 2022
commit4e70dc50580212e74aac4c12dd3255933fe85224
30 changes: 0 additions & 30 deletionsLib/test/test_ctypes/test_python_api.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -81,36 +81,6 @@ 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):
"""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.
self.assertEqual(frame.f_back, None)


if __name__ == "__main__":
unittest.main()
9 changes: 9 additions & 0 deletionsLib/test/test_frame.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -408,6 +408,15 @@ def test_frame_get_generator(self):
frame = next(gen)
self.assertIs(gen, _testcapi.frame_getgenerator(frame))

def test_frame_fback_api(self):
"""Test that accessing `f_back` does not cause a segmentation fault on
a frame created with `PyFrame_New` (GH-99110)."""
def dummy():
pass

frame = _testcapi.frame_new(dummy.__code__, globals(), locals())
# The following line should not cause a segmentation fault.
self.assertEqual(frame.f_back, None)

if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletionsModules/_testcapimodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,6 +20,7 @@
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "frameobject.h" // PyFrame_New
#include "marshal.h" // PyMarshal_WriteLongToFile
#include "structmember.h" // for offsetof(), T_OBJECT
#include <float.h> // FLT_MAX
Expand DownExpand Up@@ -2911,6 +2912,22 @@ frame_getlasti(PyObject *self, PyObject *frame)
return PyLong_FromLong(lasti);
}

static PyObject *
frame_new(PyObject *self, PyObject *args)
{
PyObject *code, *globals, *locals;
if (!PyArg_ParseTuple(args, "OOO", &code, &globals, &locals)) {
return NULL;
}
if (!PyCode_Check(code)) {
PyErr_SetString(PyExc_TypeError, "argument must be a code object");
return NULL;
}
PyThreadState *tstate = PyThreadState_Get();

return (PyObject *)PyFrame_New(tstate, (PyCodeObject *)code, globals, locals);
}

static PyObject *
test_frame_getvar(PyObject *self, PyObject *args)
{
Expand DownExpand Up@@ -3352,6 +3369,7 @@ static PyMethodDef TestMethods[] = {
{"frame_getgenerator", frame_getgenerator, METH_O, NULL},
{"frame_getbuiltins", frame_getbuiltins, METH_O, NULL},
{"frame_getlasti", frame_getlasti, METH_O, NULL},
{"frame_new", frame_new, METH_VARARGS, NULL},
{"frame_getvar", test_frame_getvar, METH_VARARGS, NULL},
{"frame_getvarstring", test_frame_getvarstring, METH_VARARGS, NULL},
{"eval_get_func_name", eval_get_func_name, METH_O, NULL},
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp