public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH] Fix crash in gdbpy_parse_register_id
Date: Wed, 27 Apr 2022 15:27:42 -0600	[thread overview]
Message-ID: <20220427212742.4003557-1-tom@tromey.com> (raw)

I noticed that gdbpy_parse_register_id would assert if passed a Python
object of a type it was not expecting.  The included test case shows
this crash.  This patch fixes the problem and also changes
gdbpy_parse_register_id to be more "Python-like" -- it always ensures
the Python error is set when it fails, and the callers now simply
propagate the existing exception.
---
 gdb/python/py-frame.c                 |  5 +----
 gdb/python/py-registers.c             | 22 +++++++++++++++-------
 gdb/python/py-unwind.c                | 10 ++--------
 gdb/python/python-internal.h          |  3 ++-
 gdb/testsuite/gdb.python/py-frame.exp |  5 +++++
 5 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index d07158a5ec6..933bdc773eb 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -253,10 +253,7 @@ frapy_read_register (PyObject *self, PyObject *args)
 
       if (!gdbpy_parse_register_id (get_frame_arch (frame), pyo_reg_id,
 				    &regnum))
-	{
-	  PyErr_SetString (PyExc_ValueError, "Bad register");
-	  return NULL;
-	}
+	return NULL;
 
       gdb_assert (regnum >= 0);
       val = value_of_register (regnum, frame);
diff --git a/gdb/python/py-registers.c b/gdb/python/py-registers.c
index bbb322f068c..c7ea529bdde 100644
--- a/gdb/python/py-registers.c
+++ b/gdb/python/py-registers.c
@@ -386,21 +386,27 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id,
 	{
 	  *reg_num = user_reg_map_name_to_regnum (gdbarch, reg_name.get (),
 						  strlen (reg_name.get ()));
-	  return *reg_num >= 0;
+	  if (*reg_num >= 0)
+	    return true;
+	  PyErr_SetString (PyExc_ValueError, "Bad register");
 	}
     }
   /* The register could be its internal GDB register number.  */
   else if (PyLong_Check (pyo_reg_id))
     {
       long value;
-      if (gdb_py_int_as_long (pyo_reg_id, &value) && (int) value == value)
+      if (gdb_py_int_as_long (pyo_reg_id, &value) == 0)
 	{
-	  if (user_reg_map_regnum_to_name (gdbarch, value) != NULL)
-	    {
-	      *reg_num = (int) value;
-	      return true;
-	    }
+	  /* Nothing -- error.  */
 	}
+      else if ((int) value == value
+	       && user_reg_map_regnum_to_name (gdbarch, value) != NULL)
+	{
+	  *reg_num = (int) value;
+	  return true;
+	}
+      else
+	PyErr_SetString (PyExc_ValueError, "Bad register");
     }
   /* The register could be a gdb.RegisterDescriptor object.  */
   else if (PyObject_IsInstance (pyo_reg_id,
@@ -417,6 +423,8 @@ gdbpy_parse_register_id (struct gdbarch *gdbarch, PyObject *pyo_reg_id,
 	PyErr_SetString (PyExc_ValueError,
 			 _("Invalid Architecture in RegisterDescriptor"));
     }
+  else
+    PyErr_SetString (PyExc_ValueError, _("Invalid type for register"));
 
   gdb_assert (PyErr_Occurred ());
   return false;
diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c
index b2fd1402e93..e2cd67a0785 100644
--- a/gdb/python/py-unwind.c
+++ b/gdb/python/py-unwind.c
@@ -262,10 +262,7 @@ unwind_infopy_add_saved_register (PyObject *self, PyObject *args)
 			  &pyo_reg_id, &pyo_reg_value))
     return NULL;
   if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, &regnum))
-    {
-      PyErr_SetString (PyExc_ValueError, "Bad register");
-      return NULL;
-    }
+    return NULL;
 
   /* If REGNUM identifies a user register then *maybe* we can convert this
      to a real (i.e. non-user) register.  The maybe qualifier is because we
@@ -383,10 +380,7 @@ pending_framepy_read_register (PyObject *self, PyObject *args)
   if (!PyArg_UnpackTuple (args, "read_register", 1, 1, &pyo_reg_id))
     return NULL;
   if (!gdbpy_parse_register_id (pending_frame->gdbarch, pyo_reg_id, &regnum))
-    {
-      PyErr_SetString (PyExc_ValueError, "Bad register");
-      return NULL;
-    }
+    PyErr_SetString (PyExc_ValueError, "Bad register");
 
   try
     {
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index d947b96033b..dffcd3f1b7f 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -799,7 +799,8 @@ typedef std::unique_ptr<Py_buffer, Py_buffer_deleter> Py_buffer_up;
 
    If a register is parsed successfully then *REG_NUM will have been
    updated, and true is returned.  Otherwise the contents of *REG_NUM are
-   undefined, and false is returned.
+   undefined, and false is returned.  When false is returned, the
+   Python error is set.
 
    The PYO_REG_ID object can be a string, the name of the register.  This
    is the slowest approach as GDB has to map the name to a number for each
diff --git a/gdb/testsuite/gdb.python/py-frame.exp b/gdb/testsuite/gdb.python/py-frame.exp
index b91ffe62a83..881219342e3 100644
--- a/gdb/testsuite/gdb.python/py-frame.exp
+++ b/gdb/testsuite/gdb.python/py-frame.exp
@@ -128,3 +128,8 @@ if { $pc != "" } {
 	" = True" \
 	"test Frame.read_register($pc)"
 }
+
+# This previously caused a crash.
+gdb_test "python print(gdb.selected_frame().read_register(list()))" \
+    ".*Invalid type for register.*" \
+    "test Frame.read_register with list"
-- 
2.34.1


             reply	other threads:[~2022-04-27 21:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27 21:27 Tom Tromey [this message]
2022-04-28  1:45 ` Simon Marchi
2022-04-28  2:11   ` Tom Tromey
2022-06-11 17:38   ` Tom Tromey
2022-08-21 14:01     ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220427212742.4003557-1-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).