[Python-Dev] PyObject_IsInstance is dangerous (original) (raw)
Serhiy Storchaka storchaka at gmail.com
Mon May 18 11:05:03 CEST 2015
- Previous message (by thread): [Python-Dev] PEP 485 isclose() implementation review requested
- Next message (by thread): [Python-Dev] PyObject_IsInstance is dangerous
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
PyObject_IsInstance is not safe when used to check if the object is an instance of specified builtin type. Typical code:
rc = PyObject_IsInstance(obj, &Someting_Type);
if (rc < 0) return NULL;
if (rc) {
SometingObject *something = (SometingObject *)obj;
something->some_field ...
}
The class attribute can be modified and PyObject_IsInstance() can return true if the object has not layout compatible with specified structure. And even worse, class can be dynamic property and PyObject_IsInstance() can execute arbitrary Python code, that can invalidate cached values of pointers and sizes in C code.
More safe way would be to use PyObject_IsSubclass().
rc = PyObject_IsSubclass((Py_Object *)obj->ob_type, &Someting_Type);
if (rc < 0) return NULL;
if (rc) {
SometingObject *something = (SometingObject *)obj;
something->some_field ...
}
For example see issue24102 [1], issue24091 [2] and many other issues opened by pkt.
[1] http://bugs.python.org/issue24102 [2] http://bugs.python.org/issue24091
- Previous message (by thread): [Python-Dev] PEP 485 isclose() implementation review requested
- Next message (by thread): [Python-Dev] PyObject_IsInstance is dangerous
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]