public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix assertion failure in copy_type
@ 2022-06-05 17:30 Tom Tromey
  2022-06-18 16:43 ` Tom Tromey
  0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2022-06-05 17:30 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

PR exp/20630 points out a simple way to cause an assertion failure in
copy_type -- but this was found in the wild a few times as well.

copy_type only works for objfile-owned types, but there isn't a deep
reason for this.  This patch fixes the bug by updating copy_type to
work for any sort of type.

Better would perhaps be to finally implement type GC, but I still
haven't attempted this.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=20630
---
 gdb/gdbtypes.c                       | 21 +++++++++------------
 gdb/testsuite/gdb.base/printcmds.exp |  3 +++
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 2a51372a037..c8f98554859 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -5796,27 +5796,24 @@ copy_type_recursive (struct objfile *objfile,
 }
 
 /* Make a copy of the given TYPE, except that the pointer & reference
-   types are not preserved.
-   
-   This function assumes that the given type has an associated objfile.
-   This objfile is used to allocate the new type.  */
+   types are not preserved.  */
 
 struct type *
 copy_type (const struct type *type)
 {
-  struct type *new_type;
-
-  gdb_assert (type->is_objfile_owned ());
-
-  new_type = alloc_type_copy (type);
+  struct type *new_type = alloc_type_copy (type);
   new_type->set_instance_flags (type->instance_flags ());
   TYPE_LENGTH (new_type) = TYPE_LENGTH (type);
   memcpy (TYPE_MAIN_TYPE (new_type), TYPE_MAIN_TYPE (type),
 	  sizeof (struct main_type));
   if (type->main_type->dyn_prop_list != NULL)
-    new_type->main_type->dyn_prop_list
-      = copy_dynamic_prop_list (&type->objfile_owner ()->objfile_obstack,
-				type->main_type->dyn_prop_list);
+    {
+      struct obstack *storage = (type->is_objfile_owned ()
+				 ? &type->objfile_owner ()->objfile_obstack
+				 : gdbarch_obstack (type->arch_owner ()));
+      new_type->main_type->dyn_prop_list
+	= copy_dynamic_prop_list (storage, type->main_type->dyn_prop_list);
+    }
 
   return new_type;
 }
diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index 3260c8a3c09..2032d9d45a7 100644
--- a/gdb/testsuite/gdb.base/printcmds.exp
+++ b/gdb/testsuite/gdb.base/printcmds.exp
@@ -752,6 +752,9 @@ proc test_print_array_constants {} {
     gdb_test_escape_braces "print {{0,1,2},{3,4,5}}"  " = {{0, 1, 2}, {3, 4, 5}}"
     gdb_test "print {4,5,6}\[2\]"	" = 6"
     gdb_test "print *&{4,5,6}\[1\]"	"Attempt to take address of value not located in memory."
+
+    # This used to cause a crash.
+    gdb_test "print {unsigned char[]}{65}" " = 65 'A'"
 }
 
 proc test_print_enums {} {
-- 
2.34.1


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

* Re: [PATCH] Fix assertion failure in copy_type
  2022-06-05 17:30 [PATCH] Fix assertion failure in copy_type Tom Tromey
@ 2022-06-18 16:43 ` Tom Tromey
  0 siblings, 0 replies; 2+ messages in thread
From: Tom Tromey @ 2022-06-18 16:43 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> PR exp/20630 points out a simple way to cause an assertion failure in
Tom> copy_type -- but this was found in the wild a few times as well.

I'm checking this in.

Tom

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

end of thread, other threads:[~2022-06-18 16:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-05 17:30 [PATCH] Fix assertion failure in copy_type Tom Tromey
2022-06-18 16:43 ` Tom Tromey

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).