bpo-28129: fix ctypes crashes (#386) (#3799) · python/cpython@7d6ddb9 (original) (raw)

`@@ -990,6 +990,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

`

990

990

`if (proto) {

`

991

991

`StgDictObject *itemdict = PyType_stgdict(proto);

`

992

992

`const char *current_format;

`

``

993

`+

/* PyCPointerType_SetProto has verified proto has a stgdict. */

`

993

994

`assert(itemdict);

`

994

995

`/* If itemdict->format is NULL, then this is a pointer to an

`

995

996

` incomplete type. We create a generic format string

`

`@@ -1036,7 +1037,11 @@ PyCPointerType_set_type(PyTypeObject *self, PyObject *type)

`

1036

1037

`StgDictObject *dict;

`

1037

1038

``

1038

1039

`dict = PyType_stgdict((PyObject *)self);

`

1039

``

`-

assert(dict);

`

``

1040

`+

if (!dict) {

`

``

1041

`+

PyErr_SetString(PyExc_TypeError,

`

``

1042

`+

"abstract class");

`

``

1043

`+

return NULL;

`

``

1044

`+

}

`

1040

1045

``

1041

1046

`if (-1 == PyCPointerType_SetProto(dict, type))

`

1042

1047

`return NULL;

`

`@@ -1062,7 +1067,11 @@ PyCPointerType_from_param(PyObject *type, PyObject *value)

`

1062

1067

` }

`

1063

1068

``

1064

1069

`typedict = PyType_stgdict(type);

`

1065

``

`-

assert(typedict); /* Cannot be NULL for pointer types */

`

``

1070

`+

if (!typedict) {

`

``

1071

`+

PyErr_SetString(PyExc_TypeError,

`

``

1072

`+

"abstract class");

`

``

1073

`+

return NULL;

`

``

1074

`+

}

`

1066

1075

``

1067

1076

`/* If we expect POINTER(), but receive a instance, accept

`

1068

1077

` it by calling byref().

`

`@@ -2127,7 +2136,11 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)

`

2127

2136

` }

`

2128

2137

``

2129

2138

`dict = PyType_stgdict(type);

`

2130

``

`-

assert(dict);

`

``

2139

`+

if (!dict) {

`

``

2140

`+

PyErr_SetString(PyExc_TypeError,

`

``

2141

`+

"abstract class");

`

``

2142

`+

return NULL;

`

``

2143

`+

}

`

2131

2144

``

2132

2145

`/* I think we can rely on this being a one-character string */

`

2133

2146

`fmt = PyUnicode_AsUTF8(dict->proto);

`

`@@ -3231,7 +3244,11 @@ _validate_paramflags(PyTypeObject *type, PyObject *paramflags)

`

3231

3244

`PyObject *argtypes;

`

3232

3245

``

3233

3246

`dict = PyType_stgdict((PyObject *)type);

`

3234

``

`-

assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */

`

``

3247

`+

if (!dict) {

`

``

3248

`+

PyErr_SetString(PyExc_TypeError,

`

``

3249

`+

"abstract class");

`

``

3250

`+

return 0;

`

``

3251

`+

}

`

3235

3252

`argtypes = dict->argtypes;

`

3236

3253

``

3237

3254

`if (paramflags == NULL || dict->argtypes == NULL)

`

`@@ -4862,7 +4879,7 @@ Pointer_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value)

`

4862

4879

` }

`

4863

4880

``

4864

4881

`stgdict = PyObject_stgdict((PyObject *)self);

`

4865

``

`-

assert(stgdict); /* Cannot be NULL fr pointer instances */

`

``

4882

`+

assert(stgdict); /* Cannot be NULL for pointer instances */

`

4866

4883

``

4867

4884

`proto = stgdict->proto;

`

4868

4885

`assert(proto);

`

`@@ -4890,7 +4907,7 @@ Pointer_get_contents(CDataObject *self, void *closure)

`

4890

4907

` }

`

4891

4908

``

4892

4909

`stgdict = PyObject_stgdict((PyObject *)self);

`

4893

``

`-

assert(stgdict); /* Cannot be NULL fr pointer instances */

`

``

4910

`+

assert(stgdict); /* Cannot be NULL for pointer instances */

`

4894

4911

`return PyCData_FromBaseObj(stgdict->proto,

`

4895

4912

` (PyObject *)self, 0,

`

4896

4913

`*(void **)self->b_ptr);

`

`@@ -4909,7 +4926,7 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)

`

4909

4926

`return -1;

`

4910

4927

` }

`

4911

4928

`stgdict = PyObject_stgdict((PyObject *)self);

`

4912

``

`-

assert(stgdict); /* Cannot be NULL fr pointer instances */

`

``

4929

`+

assert(stgdict); /* Cannot be NULL for pointer instances */

`

4913

4930

`assert(stgdict->proto);

`

4914

4931

`if (!CDataObject_Check(value)) {

`

4915

4932

`int res = PyObject_IsInstance(value, stgdict->proto);

`