Skip to content

_remote_debugging: BinaryWriter.write_sample() trivial segfault #157227

Description

@maurycy

Bug report

Bug description:

[139] 2026-09-09T18:57:09.249268000+0200 maurycy@gimel /Users/maurycy/work/cpython (main 52ffffe) % ./python.exe 
Python 3.16.0a0 (heads/main:83dbe6ae9f4, Sep  6 2026, 18:18:16) [Clang 21.0.0 (clang-2100.1.1.101)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import _remote_debugging
>>> _remote_debugging.BinaryWriter("/tmp/o.bin", 1000, 1000000).write_sample([42], 2000)
zsh: segmentation fault  ./python.exe

There's many more variants, easy to come up with.

This is the place:

if (!PyList_Check(stack_frames)) {

It only checks if it's a list, and then it trusts completely:

/* Use unchecked accessors since we control the data structures */
PyObject *frame_info = PyList_GET_ITEM(frame_list, k);
/* Get filename, location, funcname, opcode from FrameInfo using unchecked access */
PyObject *filename = PyStructSequence_GET_ITEM(frame_info, 0);
PyObject *location = PyStructSequence_GET_ITEM(frame_info, 1);
PyObject *funcname = PyStructSequence_GET_ITEM(frame_info, 2);
PyObject *opcode_obj = PyStructSequence_GET_ITEM(frame_info, 3);

PyObject *thread_id_obj = PyStructSequence_GET_ITEM(thread_info, 0);
PyObject *status_obj = PyStructSequence_GET_ITEM(thread_info, 1);
PyObject *frame_list = PyStructSequence_GET_ITEM(thread_info, 2);

PyObject *interp_info = PyList_GET_ITEM(stack_frames, i);
PyObject *interp_id_obj = PyStructSequence_GET_ITEM(interp_info, 0);
PyObject *threads = PyStructSequence_GET_ITEM(interp_info, 1);

The comment says Use unchecked accessors since we control the data structures but I still think that segmentation fault on a public method is not great.

I will submit a PR soon.

ref #148178

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions