Commits

Thomas Heller committed 24cab22

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.

  • Participants
  • Parent commits 798beca
  • Branches legacy-trunk

Comments (0)

Files changed (1)

File Modules/_ctypes/_ctypes.c

 
 	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
 
 		/* 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);
  		   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;
 	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;
 	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;
 	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);
 
 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)))
 		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,
 		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;
 		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;
 _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;
 	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;
 	}
 
 	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?
 	*/
 	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) {
 	}
 	
 	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");
 	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)
 {
 	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);
 }
 
 	}
 
 	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;
 	}
 	
 	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;
 	}
 
 	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);
 		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)) {
 	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);