Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0261e1e

Browse files
author
Thomas Heller
committed
Merged revisions 64968,64971 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r64968 | thomas.heller | 2008-07-15 19:03:08 +0200 (Di, 15 Jul 2008) | 4 lines Issue #3258: Fix an assertion error (in debug build) and a crash (in release build) when the format string of a pointer to an incomplete structure is created. ........ r64971 | thomas.heller | 2008-07-15 19:19:50 +0200 (Di, 15 Jul 2008) | 2 lines NEWS entry for #issue 3258. ........
1 parent d88ddfa commit 0261e1e

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

Lib/ctypes/test/test_pep3118.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
def normalize(format):
1313
# Remove current endian specifier and white space from a format
1414
# string
15+
if format is None:
16+
return ""
1517
format = format.replace(OTHER_ENDIAN, THIS_ENDIAN)
1618
return re.sub(r"\s", "", format)
1719

@@ -84,6 +86,14 @@ class EmptyStruct(Structure):
8486
class aUnion(Union):
8587
_fields_ = [("a", c_int)]
8688

89+
class Incomplete(Structure):
90+
pass
91+
92+
class Complete(Structure):
93+
pass
94+
PComplete = POINTER(Complete)
95+
Complete._fields_ = [("a", c_int)]
96+
8797
################################################################
8898
#
8999
# This table contains format strings as they look on little endian
@@ -141,6 +151,16 @@ class aUnion(Union):
141151
# the pep does't support unions
142152
(aUnion, "B", None, aUnion),
143153

154+
## pointer to incomplete structure
155+
(Incomplete, "B", None, Incomplete),
156+
(POINTER(Incomplete), "&B", None, POINTER(Incomplete)),
157+
158+
# 'Complete' is a structure that starts incomplete, but is completed after the
159+
# pointer type to it has been created.
160+
(Complete, "T{<l:a:}", None, Complete),
161+
# Unfortunately the pointer format string is not fixed...
162+
(POINTER(Complete), "&B", None, POINTER(Complete)),
163+
144164
## other
145165

146166
# function signatures are not implemented

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #3258: Fixed a crash when a ctypes POINTER type to an
31+
incomplete structure was created.
32+
3033
- Issue #2683: Fix inconsistency in subprocess.Popen.communicate(): the
3134
argument now must be a bytes object in any case.
3235

Modules/_ctypes/_ctypes.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isSt
353353
}
354354
Py_DECREF(result->tp_dict);
355355
result->tp_dict = (PyObject *)dict;
356+
dict->format = alloc_format_string(NULL, "B");
357+
if (dict->format == NULL) {
358+
Py_DECREF(result);
359+
return NULL;
360+
}
356361

357362
dict->paramfunc = StructUnionType_paramfunc;
358363

@@ -871,7 +876,13 @@ PointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
871876
if (proto) {
872877
StgDictObject *itemdict = PyType_stgdict(proto);
873878
assert(itemdict);
874-
stgdict->format = alloc_format_string("&", itemdict->format);
879+
/* If itemdict->format is NULL, then this is a pointer to an
880+
incomplete type. We create a generic format string
881+
'pointer to bytes' in this case. XXX Better would be to
882+
fix the format string later...
883+
*/
884+
stgdict->format = alloc_format_string("&",
885+
itemdict->format ? itemdict->format : "B");
875886
if (stgdict->format == NULL) {
876887
Py_DECREF((PyObject *)stgdict);
877888
return NULL;

0 commit comments

Comments
 (0)