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

ctypes.Array.from_buffer segmentation fault when trying to create from array.array #69845

Closed
spearsemgmailcom mannequin opened this issue Nov 18, 2015 · 4 comments
Closed
Labels
3.7 (EOL) end of life topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@spearsemgmailcom
Copy link
Mannequin

spearsemgmailcom mannequin commented Nov 18, 2015

BPO 25659
Nosy @vadmium, @eryksun, @zhangyangyu
PRs
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • from_buffer.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2016-11-21.03:32:14.620>
    created_at = <Date 2015-11-18.17:57:16.353>
    labels = ['ctypes', '3.7', 'type-crash']
    title = 'ctypes.Array.from_buffer segmentation fault when trying to create from array.array'
    updated_at = <Date 2017-03-31.16:36:26.989>
    user = 'https://bugs.python.org/spearsemgmailcom'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:26.989>
    actor = 'dstufft'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-11-21.03:32:14.620>
    closer = 'martin.panter'
    components = ['ctypes']
    creation = <Date 2015-11-18.17:57:16.353>
    creator = 'spearsem@gmail.com'
    dependencies = []
    files = ['45543']
    hgrepos = []
    issue_num = 25659
    keywords = ['patch']
    message_count = 4.0
    messages = ['254851', '254853', '281187', '281309']
    nosy_count = 5.0
    nosy_names = ['python-dev', 'martin.panter', 'eryksun', 'xiang.zhang', 'spearsem@gmail.com']
    pr_nums = ['552']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue25659'
    versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7']

    @spearsemgmailcom
    Copy link
    Mannequin Author

    spearsemgmailcom mannequin commented Nov 18, 2015

    I'm trying to find a way to create a ctypes array from the underlying memory buffer exposed by an array.array object. The ctypes.Array.from_buffer function isn't documented, but I did find the source code in _ctypes.c around line 497. It's not clear to me where the problem might be.

    Code to reproduce it below:

    import array, ctypes
    a1 = array.array('l')
    a1.fromlist(range(10))
    ctypes.Array.from_buffer(a1)
    #Segfault

    @spearsemgmailcom spearsemgmailcom mannequin added topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump labels Nov 18, 2015
    @eryksun
    Copy link
    Contributor

    eryksun commented Nov 18, 2015

    You have to subclass ctypes.Array with a _type_ and _length_. But ctypes types also implement sequence repetition (*) to facilitate creating array types. For example:

        import array, ctypes
        a1 = array.array('l')
        a1.fromlist(range(10))
        c1 = (ctypes.c_long * 10).from_buffer(a1)
        >>> c1[:]
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
        >>> c1[0] = 42
        >>> a1[0]
        42

    That said, it's way too easy to segfault this. Why not simply fail the call in this case? e.g.

        StgDictObject *dict = PyType_stgdict(type);
        if (!dict) {
            PyErr_SetString(PyExc_TypeError,
                        "abstract class");
            return NULL;
        }

    @vadmium
    Copy link
    Member

    vadmium commented Nov 19, 2016

    Here is a patch implementing Eryk’s suggestion, for both from_buffer() and from_buffer_copy() methods. This is exactly how it is already handled for Array.from_address(). The two buffer methods were added more recently than from_address().

    @vadmium vadmium added the 3.7 (EOL) end of life label Nov 19, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Nov 20, 2016

    New changeset 5f061870d49c by Martin Panter in branch '3.5':
    Issue bpo-25659: Change assert to TypeError in from_buffer/_copy()
    https://hg.python.org/cpython/rev/5f061870d49c

    New changeset 1253ef20c947 by Martin Panter in branch '3.6':
    Issue bpo-25659: Merge ctypes fix from 3.5
    https://hg.python.org/cpython/rev/1253ef20c947

    New changeset 821da4891051 by Martin Panter in branch 'default':
    Issue bpo-25659: Merge ctypes fix from 3.6
    https://hg.python.org/cpython/rev/821da4891051

    New changeset 4de956751cf1 by Martin Panter in branch '2.7':
    Issue bpo-25659: Change assert to TypeError in from_buffer/_copy()
    https://hg.python.org/cpython/rev/4de956751cf1

    @vadmium vadmium closed this as completed Nov 21, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants