Navigation Menu

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 segfaults when passing a unicode string to a function without argtypes #49453

Closed
amauryfa opened this issue Feb 10, 2009 · 2 comments
Closed
Assignees
Labels
topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@amauryfa
Copy link
Member

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']

@amauryfa
Copy link
Member Author

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();

@amauryfa amauryfa added topic-ctypes type-crash A hard crash of the interpreter, possibly with a core dump labels Feb 10, 2009
@theller
Copy link

theller commented Feb 10, 2009

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.

@theller theller closed this as completed Feb 10, 2009
@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
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