[Patches] PyObject_GetAttr/PyObject_SetAttr core dumps if given
non-string names
M.-A. Lemburg
mal@lemburg.com
Thu, 22 Jun 2000 22:10:56 +0200
Gisle Aas wrote:
>
> While developing a perl interface to python I discovered that
> PyObject_(Has|Get|Set)Attr core dumps if given non-string names.
>
> This patch ought to fix it. If you actually wanted the object passed
> to v->ob_type->tp_setattro to be interned if it was a string, then a
> little additional tweak is needed for the PyObject_SetAttr().
>
> Regards,
> Gisle Aas
>
> Index: Objects/object.c
> ===================================================================
> RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
> retrieving revision 2.71
> diff -u -p -u -r2.71 object.c
> --- Objects/object.c 2000/06/09 16:20:39 2.71
> +++ Objects/object.c 2000/06/22 19:27:51
> @@ -600,8 +600,12 @@ PyObject_GetAttr(v, name)
> {
> if (v->ob_type->tp_getattro != NULL)
> return (*v->ob_type->tp_getattro)(v, name);
> - else
> + else if (PyString_Check(name))
> return PyObject_GetAttrString(v, PyString_AsString(name));
> + else {
> + PyErr_SetString(PyExc_TypeError,
> + "attribute name must be string");
> + }
> }
Hmm, I think it would be more generic to add a NULL
check to PyObject_GetAttrString() (PyString_AsString() returns
NULL in case name is not a string object).
> int
> @@ -626,12 +630,18 @@ PyObject_SetAttr(v, name, value)
> {
> int err;
> Py_INCREF(name);
> - PyString_InternInPlace(&name);
> if (v->ob_type->tp_setattro != NULL)
> err = (*v->ob_type->tp_setattro)(v, name, value);
> - else
> + else if (PyString_Check(name)) {
> + PyString_InternInPlace(&name);
> err = PyObject_SetAttrString(
> v, PyString_AsString(name), value);
> + }
> + else {
> + PyErr_SetString(PyExc_TypeError,
> + "attribute name must be string");
> + err = -1;
> + }
Here, I think you ought to move the string test just before
the interning call.
> Py_DECREF(name);
> return err;
> }
>
> _______________________________________________
> Patches mailing list
> Patches@python.org
> http://www.python.org/mailman/listinfo/patches
--
Marc-Andre Lemburg
______________________________________________________________________
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/