From 1fc2d1491c0d512c61303f71d2ba420dd389da0f Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Tue, 22 Apr 2025 21:43:43 +0100 Subject: gdb/python: don't use PyObject_IsInstance in py-registers.c In python/py-registers.c we make use of PyObject_IsInstance. The PyObject_IsInstance can return -1 for an error, 0 for false, or 1 for true. In py-registers.c we treat the return value from PyObject_IsInstance as a boolean, which means both -1 and 1 will be treated as true. If PyObject_IsInstance returns -1 for an error, this will be treated as true, we will then invoke undefined behaviour as the pyo_reg_id object will be treated as a gdb.RegisterDescriptor, even though it might not be. I noticed that the gdb.RegisterDescriptor class does not have the Py_TPFLAGS_BASETYPE flag, and therefore cannot be inherited from. As such, using PyObject_IsInstance is not necessary, we can use PyObject_TypeCheck instead. The PyObject_TypeCheck function only returns 0 or 1, so we don't need to worry about the error case. Approved-By: Tom Tromey --- gdb/python/py-registers.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'gdb/python') diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c index 78a806c..9be2e3c 100644 --- a/gdb/python/py-registers.c +++ b/gdb/python/py-registers.c @@ -403,8 +403,7 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id, PyErr_SetString (PyExc_ValueError, "Bad register"); } /* The register could be a gdb.RegisterDescriptor object. */ - else if (PyObject_IsInstance (pyo_reg_id, - (PyObject *) ®ister_descriptor_object_type)) + else if (PyObject_TypeCheck (pyo_reg_id, ®ister_descriptor_object_type)) { register_descriptor_object *reg = (register_descriptor_object *) pyo_reg_id; -- cgit v1.1