public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] gdb: ensure the cast in gdbarch_tdep is valid
Date: Thu, 21 Jul 2022 18:20:32 +0000 (GMT)	[thread overview]
Message-ID: <20220721182032.1068D384D17C@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c44885db3b56c83a12e88a176c03f2a2268428b8

commit c44885db3b56c83a12e88a176c03f2a2268428b8
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Thu May 19 13:55:41 2022 +0100

    gdb: ensure the cast in gdbarch_tdep is valid
    
    This commit makes use of gdb::checked_static_cast when casting the
    generic gdbarch_tdep pointer to a specific sub-class type.  This means
    that, when compiled in developer mode, GDB will validate that the cast
    is correct.
    
    In order to use gdb::checked_static_cast the types involved must have
    RTTI, which is why the gdbarch_tdep base class now has a virtual
    destructor.
    
    Assuming there are no bugs in GDB where we cast a gdbarch_tdep pointer
    to the wrong type, then there should be no changes after this commit.
    
    If any bugs do exist, then GDB will now assert (in a developer build).

Diff:
---
 gdb/gdbarch.h | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index b2c91db0c4f..16c73915c64 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -28,6 +28,7 @@
 #include "infrun.h"
 #include "osabi.h"
 #include "displaced-stepping.h"
+#include "gdbsupport/gdb-checked-static-cast.h"
 
 struct floatformat;
 struct ui_file;
@@ -58,7 +59,14 @@ struct inferior;
 
 #include "regcache.h"
 
-struct gdbarch_tdep {};
+/* The base class for every architecture's tdep sub-class.  The virtual
+   destructor ensures the class has RTTI information, which allows
+   gdb::checked_static_cast to be used, the gdbarch_tdep the function.  */
+
+struct gdbarch_tdep
+{
+  virtual ~gdbarch_tdep() = default;
+};
 
 /* The architecture associated with the inferior through the
    connection to the target.
@@ -157,7 +165,7 @@ static inline TDepType *
 gdbarch_tdep (struct gdbarch *gdbarch)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep_1 (gdbarch);
-  return static_cast<TDepType *> (tdep);
+  return gdb::checked_static_cast<TDepType *> (tdep);
 }
 
 /* Mechanism for co-ordinating the selection of a specific


                 reply	other threads:[~2022-07-21 18:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220721182032.1068D384D17C@sourceware.org \
    --to=aburgess@sourceware.org \
    --cc=gdb-cvs@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).