]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/python: missing PyObject_IsTrue error check in micmdpy_set_installed
authorAndrew Burgess <aburgess@redhat.com>
Sun, 10 Nov 2024 15:50:26 +0000 (15:50 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Thu, 14 Nov 2024 19:34:44 +0000 (19:34 +0000)
Like the previous commit, I discovered that in micmdpy_set_installed
we were calling PyObject_IsTrue, but not checking for a possible error
value being returned.

The micmdpy_set_installed function implements the
gdb.MICommand.installed attribute, and the documentation indicates that
this attribute should only be assigned a bool:

  This attribute is read-write, setting this attribute to 'False'
  will uninstall the command, removing it from the set of available
  commands.  Setting this attribute to 'True' will install the
  command for use.

So I propose that instead of using PyObject_IsTrue we use
PyBool_Check, and if the new value fails this check we raise an
error.  We can then compare the new value to Py_True directly instead
of calling PyObject_IsTrue.

This is a potentially breaking change to the Python API, but hopefully
this will not impact too many people, and the fix is pretty
easy (switch to using a bool).  I've added a NEWS entry to draw
attention to this change.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/NEWS
gdb/python/py-micmd.c
gdb/testsuite/gdb.python/py-mi-cmd.exp

index b96ac5e6b79f11fe58fa7001807eaedfd23e329b..647b7256b462ed92c6dd7526c578a4d34ae349da 100644 (file)
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -72,6 +72,9 @@
   ** The 'signed' argument to gdb.Architecture.integer_type() will no
      longer accept non-bool types.
 
+  ** The gdb.MICommand.installed property can only be set to True or
+     False.
+
 * Debugger Adapter Protocol changes
 
   ** The "scopes" request will now return a scope holding global
index f4abf2b1cd0baecdc9fd9523f3a035159878a196..e051b90d4a3511dc16149cd3ae7f949510c7154c 100644 (file)
@@ -509,7 +509,16 @@ micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
 {
   struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
 
-  bool installed_p = PyObject_IsTrue (newvalue);
+  if (!PyBool_Check (newvalue))
+    {
+      PyErr_Format (PyExc_TypeError,
+                   _("gdb.MICommand.installed must be set to a bool, not %s"),
+                   newvalue == Py_None ? "None" : Py_TYPE(newvalue)->tp_name);
+      return -1;
+    }
+
+  bool installed_p = newvalue == Py_True;
+
   if (installed_p == (micmd_obj->mi_command != nullptr))
     return 0;
 
index 52914099e6df59854befc5f661cb6d67f39815b6..db017c6cdf91f1146ad1d218ff48b778a6879521 100644 (file)
@@ -233,6 +233,21 @@ mi_gdb_test "-abc str" \
     "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
     "-abc str, but now the command is gone"
 
+mi_gdb_test "python cmd.installed = None" \
+    ".*\r\n\\^error,msg=\"Error occurred in Python: gdb\\.MICommand\\.installed must be set to a bool, not None\"" \
+    "re-install the mi command using value None"
+
+mi_gdb_test "python cmd.installed = 1" \
+    ".*\r\n\\^error,msg=\"Error occurred in Python: gdb\\.MICommand\\.installed must be set to a bool, not int\"" \
+    "re-install the mi command using an int value"
+
+mi_gdb_test "python print(cmd.installed)" \
+    [multi_line \
+        ".*" \
+        "~\"False\\\\n\"" \
+        "\\^done"] \
+    "cmd is still not installed"
+
 mi_gdb_test "python cmd.installed = True" \
     ".*\\^done" \
     "re-install the mi command"