This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: PyMapping_Check crashes when argument is NULL
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: mdehoon, rhettinger
Priority: normal Keywords:

Created on 2004-09-19 04:22 by mdehoon, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg22485 - (view) Author: Michiel de Hoon (mdehoon) * Date: 2004-09-19 04:22
The function PyMapping_Check in Objects/abstract.c in
Python 2.4a3 is

int
PyMapping_Check(PyObject *o)
{
        if (PyInstance_Check(o))
                return PyObject_HasAttrString(o,
"__getitem__");

        return  o && o->ob_type->tp_as_mapping &&
                o->ob_type->tp_as_mapping->mp_subscript &&
                !(o->ob_type->tp_as_sequence &&
                  o->ob_type->tp_as_sequence->sq_slice);
}

where PyInstance_Check is #defined in
Include/classobject.h as

#define PyInstance_Check(op) ((op)->ob_type ==
&PyInstance_Type)

Hence, if the argument o of PyMapping_Check is NULL,
the function will crash. I first noticed this crash on
Cygwin.
The problem is that PyInstance_Check dereferences the
argument o before the check for o==NULL is made in the
return statement.

In Python 2.3.4, PyMapping_Check was

int
PyMapping_Check(PyObject *o)
{
        return o && o->ob_type->tp_as_mapping &&
                o->ob_type->tp_as_mapping->mp_subscript;
}

which checks for o==NULL correctly.

By the way, I am not sure if I am submitting this to
the correct category.
msg22486 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-09-19 06:00
Logged In: YES 
user_id=80475

It is not clear that this is actually a bug.  Most of the
PY???_Check functions expect a non-NULL object.  The C API
docs are silent on the subject. 

However, in the spirit of defensive programming, I added the
early null checks back to PyMapping_Check and PySequence_Check.

Fixed and checked in as:
   Objects/abstract.c 2.132
History
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 40929
2004-09-19 04:22:32mdehooncreate