Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Phil Muldoon <pmuldoon@redhat.com>
To: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: [patch] [python] PR python/15461 (gate architecture calls)
Date: Wed, 28 Aug 2013 12:04:00 -0000	[thread overview]
Message-ID: <521DE761.6010403@redhat.com> (raw)


This patch gates calls to architecture.name and
architecture.disassemble.  If the underlying GDB architecture is NULL,
calls to those Python functions will result in an assert call.

Something I thought about is not allowing gdb.Architecture to be
instantiated directly, but I could not think of a clean way to do
this.  Also, all other GDB Python classes can be instantiated
(though, not very usefully, IE foo = gdb.Frame())

In any case, this patch gates those calls mentioned above.

Cheers,

Phil

2013-08-28  Phil Muldoon  <pmuldoon@redhat.com>

	PR python/15461

	* python/py-arch.c (ARCHPY_REQUIRE_VALID): New macro.
	(archpy_name): Check for valid architecture.
	(archpy_disassemble): Ditto.


2013-08-28  Phil Muldoon  <pmuldoon@redhat.com>

	* gdb.python/py-arch.exp: Tests for invalid architecture.

--

diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index 7098a8a..2775c9d 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -30,6 +30,14 @@ typedef struct arch_object_type_object {
 
 static struct gdbarch_data *arch_object_data = NULL;
 
+/* Require a valid Architecture.  */
+#define ARCHPY_REQUIRE_VALID (arch_obj, arch)		\
+  do {							\
+    arch = arch_object_to_gdbarch (arch_obj);		\
+    if (arch == NULL)					\
+      error (_("Architecture is invalid."));		\
+  } while (0)
+
 static PyTypeObject arch_object_type
     CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("arch_object");
 
@@ -82,9 +90,19 @@ gdbarch_to_arch_object (struct gdbarch *gdbarch)
 static PyObject *
 archpy_name (PyObject *self, PyObject *args)
 {
-  struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
-  const char *name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
-  PyObject *py_name = PyString_FromString (name);
+  struct gdbarch *gdbarch = NULL;
+  const char *name;
+  PyObject *py_name;
+  volatile struct gdb_exception except;
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      ARCHPY_REQUIRE_VALID (self, gdbarch);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  name = (gdbarch_bfd_arch_info (gdbarch))->printable_name;
+  py_name = PyString_FromString (name);
 
   return py_name;
 }
@@ -104,7 +122,14 @@ archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
   gdb_py_ulongest start_temp;
   long count = 0, i;
   PyObject *result_list, *end_obj = NULL, *count_obj = NULL;
-  struct gdbarch *gdbarch = arch_object_to_gdbarch (self);
+  struct gdbarch *gdbarch = NULL;
+  volatile struct gdb_exception except;
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      ARCHPY_REQUIRE_VALID (self, gdbarch);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
 
   if (!PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", keywords,
                                     &start_temp, &end_obj, &count_obj))
diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/py-arch.exp
index 4e736b8..0d32ef7 100644
--- a/gdb/testsuite/gdb.python/py-arch.exp
+++ b/gdb/testsuite/gdb.python/py-arch.exp
@@ -12,6 +12,7 @@
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+load_lib gdb-python.exp
 
 standard_testfile
 
@@ -26,6 +27,14 @@ if ![runto_main] {
    return -1
 }
 
+# Test python/15461.  Invalid architectures should not trigger an
+# internal GDB assert.
+gdb_py_test_silent_cmd "python empty = gdb.Architecture()" "get empty arch" 0
+gdb_test "python print(empty.name())" ".*Architecture is invalid.*" \
+    "Test empty architecture.name does not trigger an assert"
+gdb_test "python print(empty.disassemble())" ".*Architecture is invalid.*" \
+    "Test empty architecture.disassemble does not trigger an assert"
+
 gdb_py_test_silent_cmd "python frame = gdb.selected_frame()" "get frame" 0
 gdb_py_test_silent_cmd "python arch = frame.architecture()" "get arch" 0
 gdb_py_test_silent_cmd "python pc = frame.pc()" "get pc" 0

	


             reply	other threads:[~2013-08-28 12:04 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-28 12:04 Phil Muldoon [this message]
2013-08-28 14:52 ` Tom Tromey
2013-08-28 15:15   ` Phil Muldoon
2013-08-28 15:54     ` Tom Tromey
2013-08-29 11:15       ` Phil Muldoon
2013-08-29 14:58         ` Tom Tromey
2013-08-30 10:13           ` Phil Muldoon
2013-08-28 17:54 ` Tom Tromey
2013-08-28 18:14   ` Paul_Koning
2013-08-28 18:45     ` Phil Muldoon

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=521DE761.6010403@redhat.com \
    --to=pmuldoon@redhat.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