Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] gdb: tighten assertions in set_type_vptr_*
@ 2026-01-26 15:54 Simon Marchi
  2026-02-02 17:38 ` Kevin Buettner
  0 siblings, 1 reply; 4+ messages in thread
From: Simon Marchi @ 2026-01-26 15:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@polymtl.ca>

In C++, only structures and classes (represented by TYPE_CODE_STRUCT)
can participate in inheritance.  I therefore think it does not make
sense to allow for TYPE_CODE_UNION in set_type_vptr_basetype and
set_type_vptr_fieldno.  Remove the possibility for the type to be a
union in these functions.

Also, for the same reason, add an assertion that checks the type of
basetype in set_type_vptr_basetype.

I did not change the getters (internal_type_vptr_fieldno and
internal_type_vptr_basetype), because it seems like they are called by
code that handles similarly both structures and unions.  Making those
stricter would require adding conditions in those callers, which doesn't
look like an improvement.  For unions, they will correctly return an
"invalid" value.

Change allocate_cplus_struct_type to not use set_type_vptr_fieldno to
initialize the field to -1, otherwise it would trip the assertion when
initializing for a union type.

Change-Id: Id9b2dc288f24d50eb50da46782b5ec6de5682e81
---
 gdb/gdbtypes.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 75404d00e1ac..bd9abc7e7004 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1805,8 +1805,7 @@ void
 set_type_vptr_fieldno (struct type *type, int fieldno)
 {
   type = check_typedef (type);
-  gdb_assert (type->code () == TYPE_CODE_STRUCT
-	      || type->code () == TYPE_CODE_UNION);
+  gdb_assert (type->code () == TYPE_CODE_STRUCT);
   if (!HAVE_CPLUS_STRUCT (type))
     ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
@@ -1831,8 +1830,8 @@ void
 set_type_vptr_basetype (struct type *type, struct type *basetype)
 {
   type = check_typedef (type);
-  gdb_assert (type->code () == TYPE_CODE_STRUCT
-	      || type->code () == TYPE_CODE_UNION);
+  gdb_assert (type->code () == TYPE_CODE_STRUCT);
+  gdb_assert (check_typedef (basetype)->code () == TYPE_CODE_STRUCT);
   if (!HAVE_CPLUS_STRUCT (type))
     ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
@@ -3320,7 +3319,7 @@ allocate_cplus_struct_type (struct type *type)
   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
     TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
-  set_type_vptr_fieldno (type, -1);
+  TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = -1;
 }
 
 const struct gnat_aux_type gnat_aux_default =

base-commit: 6660ba84d5484a51e09985c741091152f9febe5c
-- 
2.52.0


^ permalink raw reply	[flat|nested] 4+ messages in thread
* [PATCH] gdb: tighten assertions in set_type_vptr_*
@ 2026-01-23 19:22 simon.marchi
  0 siblings, 0 replies; 4+ messages in thread
From: simon.marchi @ 2026-01-23 19:22 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@polymtl.ca>

In C++, only structures and classes (represented by TYPE_CODE_STRUCT)
can participate in inheritance.  I therefore think it does not make
sense to allow for TYPE_CODE_UNION in set_type_vptr_basetype and
set_type_vptr_fieldno.  Remove the possibility for the type to be a
union in these functions.

Also, for the same reason, add an assertion that checks the type of
basetype in set_type_vptr_basetype.

I did not change the getters (internal_type_vptr_fieldno and
internal_type_vptr_basetype), because it seems like they are called by
code that handles similarly both structures and unions.  Making those
stricter would require adding conditions in those callers, which doesn't
look like an improvement.

Change allocate_cplus_struct_type to not use set_type_vptr_fieldno to
initialize the field to -1, otherwise it would trip the assertion when
initializing for a union type.

I stumbled on this while reviewing this change:

https://inbox.sourceware.org/gdb-patches/faa28230-4665-46f8-8d86-4c99262628d4@simark.ca/T/#t

Change-Id: Id9b2dc288f24d50eb50da46782b5ec6de5682e81
---
 gdb/gdbtypes.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 75404d00e1ac..bd9abc7e7004 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1805,8 +1805,7 @@ void
 set_type_vptr_fieldno (struct type *type, int fieldno)
 {
   type = check_typedef (type);
-  gdb_assert (type->code () == TYPE_CODE_STRUCT
-	      || type->code () == TYPE_CODE_UNION);
+  gdb_assert (type->code () == TYPE_CODE_STRUCT);
   if (!HAVE_CPLUS_STRUCT (type))
     ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = fieldno;
@@ -1831,8 +1830,8 @@ void
 set_type_vptr_basetype (struct type *type, struct type *basetype)
 {
   type = check_typedef (type);
-  gdb_assert (type->code () == TYPE_CODE_STRUCT
-	      || type->code () == TYPE_CODE_UNION);
+  gdb_assert (type->code () == TYPE_CODE_STRUCT);
+  gdb_assert (check_typedef (basetype)->code () == TYPE_CODE_STRUCT);
   if (!HAVE_CPLUS_STRUCT (type))
     ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_basetype = basetype;
@@ -3320,7 +3319,7 @@ allocate_cplus_struct_type (struct type *type)
   TYPE_RAW_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
     TYPE_ZALLOC (type, sizeof (struct cplus_struct_type));
   *(TYPE_RAW_CPLUS_SPECIFIC (type)) = cplus_struct_default;
-  set_type_vptr_fieldno (type, -1);
+  TYPE_RAW_CPLUS_SPECIFIC (type)->vptr_fieldno = -1;
 }
 
 const struct gnat_aux_type gnat_aux_default =

base-commit: 329a53a6d590e2e90f590c89473990040a86c8e0
-- 
2.52.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-02-02 18:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-26 15:54 [PATCH] gdb: tighten assertions in set_type_vptr_* Simon Marchi
2026-02-02 17:38 ` Kevin Buettner
2026-02-02 18:15   ` Simon Marchi
  -- strict thread matches above, loose matches on Subject: below --
2026-01-23 19:22 simon.marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox