gdbsupport: allow passing nullptr to checked_static_cast
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 31 Jan 2023 15:57:20 +0000 (10:57 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 31 Jan 2023 17:36:46 +0000 (12:36 -0500)
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

index cc298733fadb61dd20755298c2b41fc9dc2a535b..bc75244bddd0eb50ac73905207a874f9913f9511 100644 (file)
@@ -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