[PATCH] Require a command argument in gdb.execute_mi

Tom Tromey tromey@adacore.com
Tue Oct 15 16:51:01 GMT 2024


Hannes pointed out that gdb.execute_mi() will crash.
This patch fixes the bug.
---
 gdb/python/py-mi.c                      | 7 +++++++
 gdb/testsuite/gdb.python/py-exec-mi.exp | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/gdb/python/py-mi.c b/gdb/python/py-mi.c
index f0e28d9d818..cf75a18958e 100644
--- a/gdb/python/py-mi.c
+++ b/gdb/python/py-mi.c
@@ -143,6 +143,13 @@ gdbpy_execute_mi_command (PyObject *self, PyObject *args, PyObject *kw)
   if (n_args < 0)
     return nullptr;
 
+  if (n_args == 0)
+    {
+      PyErr_SetString (PyExc_TypeError,
+		       _("gdb.execute_mi requires command argument"));
+      return nullptr;
+    }
+
   for (Py_ssize_t i = 0; i < n_args; ++i)
     {
       /* Note this returns a borrowed reference.  */
diff --git a/gdb/testsuite/gdb.python/py-exec-mi.exp b/gdb/testsuite/gdb.python/py-exec-mi.exp
index 8a5d0c9da2f..6b81644c0c7 100644
--- a/gdb/testsuite/gdb.python/py-exec-mi.exp
+++ b/gdb/testsuite/gdb.python/py-exec-mi.exp
@@ -30,3 +30,7 @@ gdb_test_no_output "python gdb.execute_mi('-exec-arguments', 'a', 'b', 'c')" \
     "set arguments"
 
 gdb_test "show args" ".*\"a b c\"."
+
+# Ensure that this causes an error, not a crash.
+gdb_test "python gdb.execute_mi()" \
+    "Error occurred in Python: gdb.execute_mi requires command argument"
-- 
2.47.0



More information about the Gdb-patches mailing list