[1/2] gdbsupport: allow passing nullptr to checked_static_cast

Message ID 20230131155721.21847-1-simon.marchi@efficios.com
State Committed
Commit d7789889b1839c3c0f64c6738b0d8517ccead049
Headers
Series [1/2] gdbsupport: allow passing nullptr to checked_static_cast |

Commit Message

Simon Marchi Jan. 31, 2023, 3:57 p.m. UTC
  Both static_cast and dynamic_cast handle nullptr (they return nullptr),
so I think checked_static_cast should too.  This will allow doing a null
check after a checked_static_cast:

  cooked_index_vector *table
    = (gdb::checked_static_cast<cooked_index_vector *>
       (per_bfd->index_table.get ()));
  if (table != nullptr)
    return;

Change-Id: If5c3134e63696f8e417c87b5f3901240c9f7ea97
---
 gdbsupport/gdb-checked-static-cast.h | 3 +++
 1 file changed, 3 insertions(+)
  

Comments

Tom Tromey Jan. 31, 2023, 5:35 p.m. UTC | #1
>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

Simon> Both static_cast and dynamic_cast handle nullptr (they return nullptr),
Simon> so I think checked_static_cast should too.  This will allow doing a null
Simon> check after a checked_static_cast:

Thanks, I think this is a good idea.

Tom
  

Patch

diff --git a/gdbsupport/gdb-checked-static-cast.h b/gdbsupport/gdb-checked-static-cast.h
index cc298733fadb..bc75244bddd0 100644
--- a/gdbsupport/gdb-checked-static-cast.h
+++ b/gdbsupport/gdb-checked-static-cast.h
@@ -54,6 +54,9 @@  checked_static_cast (V *v)
 		 "types must be related");
 
 #ifdef DEVELOPMENT
+  if (v == nullptr)
+    return nullptr;
+
   T result = dynamic_cast<T> (v);
   gdb_assert (result != nullptr);
 #else