[New-bugs-announce] [issue5203] ctypes segfaults when passing a unicode string to a function without argtypes

Amaury Forgeot d'Arc report at bugs.python.org
Tue Feb 10 13:48:04 CET 2009


New submission from Amaury Forgeot d'Arc <amauryfa at gmail.com>:

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

----------
assignee: theller
components: ctypes
messages: 81544
nosy: amaury.forgeotdarc, theller
severity: normal
status: open
title: ctypes segfaults when passing a unicode string to a function without argtypes
type: crash
versions: Python 2.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5203>
_______________________________________


More information about the New-bugs-announce mailing list