Add commented assert statements to check that the result of

PyObject_stgdict() and PyType_stgdict() calls are non-NULL before
dereferencing the result.  Hopefully this fixes what klocwork is
complaining about.

Fix a few other nits as well.
This commit is contained in:
Thomas Heller 2006-08-16 13:03:11 +00:00
parent a09fd6efdd
commit d32e616722
1 changed files with 36 additions and 20 deletions

View File

@ -152,7 +152,7 @@ StructUnionType_paramfunc(CDataObject *self)
parg->tag = 'V';
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict);
assert(stgdict); /* Cannot be NULL for structure/union instances */
parg->pffi_type = &stgdict->ffi_type_pointer;
/* For structure parameters (by value), parg->value doesn't contain the structure
data itself, instead parg->value.p *points* to the structure's data
@ -328,7 +328,6 @@ CDataType_from_param(PyObject *type, PyObject *value)
/* If we got a PyCArgObject, we must check if the object packed in it
is an instance of the type's dict->proto */
// if(dict && ob && dict->proto == (PyObject *)ob->ob_type){
if(dict && ob
&& PyObject_IsInstance(ob, dict->proto)) {
Py_INCREF(value);
@ -693,6 +692,7 @@ PointerType_from_param(PyObject *type, PyObject *value)
the item types are the same.
*/
StgDictObject *v = PyObject_stgdict(value);
assert(v); /* Cannot be NULL for pointer or array objects */
if (PyObject_IsSubclass(v->proto, typedict->proto)) {
Py_INCREF(value);
return value;
@ -1154,7 +1154,9 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
/* c_wchar array instance or pointer(c_wchar(...)) */
StgDictObject *dt = PyObject_stgdict(value);
StgDictObject *dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
StgDictObject *dict;
assert(dt); /* Cannot be NULL for pointer or array objects */
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
if (dict && (dict->setfunc == getentry("u")->setfunc)) {
Py_INCREF(value);
return value;
@ -1216,7 +1218,9 @@ c_char_p_from_param(PyObject *type, PyObject *value)
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
/* c_char array instance or pointer(c_char(...)) */
StgDictObject *dt = PyObject_stgdict(value);
StgDictObject *dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
StgDictObject *dict;
assert(dt); /* Cannot be NULL for pointer or array objects */
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
if (dict && (dict->setfunc == getentry("c")->setfunc)) {
Py_INCREF(value);
return value;
@ -1468,7 +1472,7 @@ SimpleType_paramfunc(CDataObject *self)
struct fielddesc *fd;
dict = PyObject_stgdict((PyObject *)self);
assert(dict);
assert(dict); /* Cannot be NULL for CDataObject instances */
fmt = PyString_AsString(dict->proto);
assert(fmt);
@ -2066,6 +2070,7 @@ static int
CData_clear(CDataObject *self)
{
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
Py_CLEAR(self->b_objects);
if ((self->b_needsfree)
&& ((size_t)dict->size > sizeof(self->b_value)))
@ -2363,7 +2368,9 @@ _CData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
StgDictObject *p1, *p2;
PyObject *keep;
p1 = PyObject_stgdict(value);
assert(p1); /* Cannot be NULL for array instances */
p2 = PyType_stgdict(type);
assert(p2); /* Cannot be NULL for pointer types */
if (p1->proto != p2->proto) {
PyErr_Format(PyExc_TypeError,
@ -2512,7 +2519,7 @@ CFuncPtr_get_restype(CFuncPtrObject *self)
return self->restype;
}
dict = PyObject_stgdict((PyObject *)self);
assert(dict);
assert(dict); /* Cannot be NULL for CFuncPtrObject instances */
if (dict->restype) {
Py_INCREF(dict->restype);
return dict->restype;
@ -2554,7 +2561,7 @@ CFuncPtr_get_argtypes(CFuncPtrObject *self)
return self->argtypes;
}
dict = PyObject_stgdict((PyObject *)self);
assert(dict);
assert(dict); /* Cannot be NULL for CFuncPtrObject instances */
if (dict->argtypes) {
Py_INCREF(dict->argtypes);
return dict->argtypes;
@ -2647,8 +2654,12 @@ static int
_validate_paramflags(PyTypeObject *type, PyObject *paramflags)
{
int i, len;
StgDictObject *dict = PyType_stgdict((PyObject *)type);
PyObject *argtypes = dict->argtypes;
StgDictObject *dict;
PyObject *argtypes;
dict = PyType_stgdict((PyObject *)type);
assert(dict); /* Cannot be NULL. 'type' is a CFuncPtr type. */
argtypes = dict->argtypes;
if (paramflags == NULL || dict->argtypes == NULL)
return 1;
@ -3260,7 +3271,7 @@ CFuncPtr_call(CFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
int outmask;
unsigned int numretvals;
assert(dict); /* if not, it's a bug */
assert(dict); /* Cannot be NULL for CFuncPtrObject instances */
restype = self->restype ? self->restype : dict->restype;
converters = self->converters ? self->converters : dict->converters;
checker = self->checker ? self->checker : dict->checker;
@ -3681,7 +3692,7 @@ Array_item(PyObject *_self, Py_ssize_t index)
}
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict);
assert(stgdict); /* Cannot be NULL for array instances */
/* Would it be clearer if we got the item size from
stgdict->proto's stgdict?
*/
@ -3712,6 +3723,7 @@ Array_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh)
len = ihigh - ilow;
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for array object instances */
proto = stgdict->proto;
itemdict = PyType_stgdict(proto);
if (itemdict->getfunc == getentry("c")->getfunc) {
@ -3750,6 +3762,7 @@ Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
}
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL for array object instances */
if (index < 0 || index >= stgdict->length) {
PyErr_SetString(PyExc_IndexError,
"invalid index");
@ -3941,6 +3954,7 @@ Simple_set_value(CDataObject *self, PyObject *value)
PyObject *result;
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
assert(dict->setfunc);
result = dict->setfunc(self->b_ptr, value, dict->size);
if (!result)
@ -3966,8 +3980,8 @@ Simple_get_value(CDataObject *self)
{
StgDictObject *dict;
dict = PyObject_stgdict((PyObject *)self);
assert(dict); /* Cannot be NULL for CDataObject instances */
assert(dict->getfunc);
dict = PyObject_stgdict((PyObject *)self);
return dict->getfunc(self->b_ptr, self->b_size);
}
@ -4140,11 +4154,10 @@ Pointer_item(PyObject *_self, Py_ssize_t index)
}
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict);
assert(stgdict->proto);
assert(stgdict); /* Cannot be NULL for pointer object instances */
proto = stgdict->proto;
/* XXXXXX MAKE SURE PROTO IS NOT NULL! */
assert(proto);
itemdict = PyType_stgdict(proto);
size = itemdict->size;
offset = index * itemdict->size;
@ -4175,11 +4188,11 @@ Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
}
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict);
assert(stgdict->proto);
assert(stgdict); /* Cannot be NULL fr pointer instances */
proto = stgdict->proto;
/* XXXXXX MAKE SURE PROTO IS NOT NULL! */
assert(proto);
itemdict = PyType_stgdict(proto);
size = itemdict->size;
offset = index * itemdict->size;
@ -4200,7 +4213,7 @@ Pointer_get_contents(CDataObject *self, void *closure)
}
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict);
assert(stgdict); /* Cannot be NULL fr pointer instances */
return CData_FromBaseObj(stgdict->proto,
(PyObject *)self, 0,
*(void **)self->b_ptr);
@ -4219,7 +4232,7 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)
return -1;
}
stgdict = PyObject_stgdict((PyObject *)self);
/* should have been catched in Pointer_new() */
assert(stgdict); /* Cannot be NULL fr pointer instances */
assert(stgdict->proto);
if (!CDataObject_Check(value)
|| 0 == PyObject_IsInstance(value, stgdict->proto)) {
@ -4295,8 +4308,11 @@ Pointer_slice(PyObject *_self, Py_ssize_t ilow, Py_ssize_t ihigh)
len = ihigh - ilow;
stgdict = PyObject_stgdict((PyObject *)self);
assert(stgdict); /* Cannot be NULL fr pointer instances */
proto = stgdict->proto;
assert(proto);
itemdict = PyType_stgdict(proto);
assert(itemdict);
if (itemdict->getfunc == getentry("c")->getfunc) {
char *ptr = *(char **)self->b_ptr;
return PyString_FromStringAndSize(ptr + ilow, len);