Skip to content

ctypes segfaults when passing a unicode string to a function without argtypes #49453

Closed

Description

@amauryfa
BPO 5203
Nosy @theller, @amauryfa

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 = 'https://github.com/theller'
closed_at = <Date 2009-02-10.19:07:27.013>
created_at = <Date 2009-02-10.12:48:03.105>
labels = ['ctypes', 'type-crash']
title = 'ctypes segfaults when passing a unicode string to a function without argtypes'
updated_at = <Date 2009-02-10.19:07:27.011>
user = 'https://github.com/amauryfa'

bugs.python.org fields:

activity = <Date 2009-02-10.19:07:27.011>
actor = 'theller'
assignee = 'theller'
closed = True
closed_date = <Date 2009-02-10.19:07:27.013>
closer = 'theller'
components = ['ctypes']
creation = <Date 2009-02-10.12:48:03.105>
creator = 'amaury.forgeotdarc'
dependencies = []
files = []
hgrepos = []
issue_num = 5203
keywords = []
message_count = 2.0
messages = ['81544', '81582']
nosy_count = 2.0
nosy_names = ['theller', 'amaury.forgeotdarc']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue5203'
versions = ['Python 2.6', 'Python 2.7']

Activity

  1. amauryfa commented on Feb 10, 2009

    @amauryfa
    ContributorAuthor

    The following code segfaults on platforms where HAVE_USABLE_WCHAR_T is
    not defined (for example: narrow unicode build and sizeof(wchar_t)==4)

    >> from ctypes import *
    >> import ctypes.util
    >> CDLL(ctypes.util.find_library('c')).wcslen(u'text')

    (it works if the argtypes member is defined)

    The reason is a non initialized structure, and the correction is trivial:

    --- Modules/_ctypes/callproc.c~ 2007-06-15 19:10:41.000000000 +0200
    +++ Modules/_ctypes/callproc.c  2009-02-10 13:28:10.000000000 +0100
    @@ -538,6 +538,7 @@
                    int size = PyUnicode_GET_SIZE(obj);
                    size += 1; /* terminating NUL */
                    size *= sizeof(wchar_t);
    +               pa->ffi_type = &ffi_type_pointer;
                    pa->value.p = PyMem_Malloc(size);
                    if (!pa->value.p) {
                            PyErr_NoMemory();
  2. theller commented on Feb 10, 2009

    @theller

    Fixed in trunk (rev 69505) and release26-maint (rev 69506).
    The bug was already fixed in a different way in py3k and release30-maint
    although in a different way, I merged this exact fix anyway (rev 69507
    and rev 69508).

    Thanks.

  3. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

topic-ctypestype-crashA hard crash of the interpreter, possibly with a core dump

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions