Fix crash in cp_print_value_fields
[external/binutils.git] / gdb / cp-valprint.c
index 143bb48..8feaae0 100644 (file)
@@ -1,6 +1,6 @@
 /* Support for printing C++ values for GDB, the GNU debugger.
 
-   Copyright (C) 1986-2017 Free Software Foundation, Inc.
+   Copyright (C) 1986-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -35,6 +35,7 @@
 #include "language.h"
 #include "extension.h"
 #include "typeprint.h"
+#include "common/byte-vector.h"
 
 /* Controls printing of vtbl's.  */
 static void
@@ -73,8 +74,6 @@ static struct obstack dont_print_vb_obstack;
 static struct obstack dont_print_statmem_obstack;
 static struct obstack dont_print_stat_array_obstack;
 
-extern void _initialize_cp_valprint (void);
-
 static void cp_print_static_field (struct type *, struct value *,
                                   struct ui_file *, int,
                                   const struct value_print_options *);
@@ -96,7 +95,7 @@ extern const char vtbl_ptr_name[] = "__vtbl_ptr_type";
 int
 cp_is_vtbl_ptr_type (struct type *type)
 {
-  const char *type_name = type_name_no_tag (type);
+  const char *type_name = TYPE_NAME (type);
 
   return (type_name != NULL && !strcmp (type_name, vtbl_ptr_name));
 }
@@ -244,7 +243,7 @@ cp_print_value_fields (struct type *type, struct type *real_type,
                  fprintf_filtered (stream, "\n");
                  print_spaces_filtered (2 + 2 * recurse, stream);
                  fputs_filtered ("members of ", stream);
-                 fputs_filtered (type_name_no_tag (type), stream);
+                 fputs_filtered (TYPE_NAME (type), stream);
                  fputs_filtered (": ", stream);
                }
            }
@@ -315,13 +314,14 @@ cp_print_value_fields (struct type *type, struct type *real_type,
                }
              else if (field_is_static (&TYPE_FIELD (type, i)))
                {
-                 struct value *v = NULL;
-
                  TRY
                    {
-                     v = value_static_field (type, i);
-                   }
+                     struct value *v = value_static_field (type, i);
 
+                     cp_print_static_field (TYPE_FIELD_TYPE (type, i),
+                                            v, stream, recurse + 1,
+                                            options);
+                   }
                  CATCH (ex, RETURN_MASK_ERROR)
                    {
                      fprintf_filtered (stream,
@@ -329,10 +329,6 @@ cp_print_value_fields (struct type *type, struct type *real_type,
                                        ex.message);
                    }
                  END_CATCH
-
-                 cp_print_static_field (TYPE_FIELD_TYPE (type, i),
-                                        v, stream, recurse + 1,
-                                        options);
                }
              else if (i == vptr_fieldno && type == vptr_basetype)
                {
@@ -372,18 +368,14 @@ cp_print_value_fields (struct type *type, struct type *real_type,
          if (obstack_final_size > statmem_obstack_initial_size)
            {
              /* In effect, a pop of the printed-statics stack.  */
-
-             void *free_to_ptr =
-               (char *) obstack_next_free (&dont_print_statmem_obstack) -
-               (obstack_final_size - statmem_obstack_initial_size);
-
-             obstack_free (&dont_print_statmem_obstack,
-                           free_to_ptr);
+             size_t shrink_bytes
+               = statmem_obstack_initial_size - obstack_final_size;
+             obstack_blank_fast (&dont_print_statmem_obstack, shrink_bytes);
            }
 
          if (last_set_recurse != recurse)
            {
-             size_t obstack_final_size =
+             obstack_final_size =
                obstack_object_size (&dont_print_stat_array_obstack);
              
              if (obstack_final_size > stat_array_obstack_initial_size)
@@ -534,23 +526,18 @@ cp_print_value (struct type *type, struct type *real_type,
              if ((boffset + offset) < 0
                  || (boffset + offset) >= TYPE_LENGTH (real_type))
                {
-                 gdb_byte *buf;
-                 struct cleanup *back_to;
-
-                 buf = (gdb_byte *) xmalloc (TYPE_LENGTH (baseclass));
-                 back_to = make_cleanup (xfree, buf);
+                 gdb::byte_vector buf (TYPE_LENGTH (baseclass));
 
-                 if (target_read_memory (address + boffset, buf,
+                 if (target_read_memory (address + boffset, buf.data (),
                                          TYPE_LENGTH (baseclass)) != 0)
                    skip = 1;
                  base_val = value_from_contents_and_address (baseclass,
-                                                             buf,
+                                                             buf.data (),
                                                              address + boffset);
                  baseclass = value_type (base_val);
                  thisoffset = 0;
                  boffset = 0;
                  thistype = baseclass;
-                 do_cleanups (back_to);
                }
              else
                {
@@ -643,7 +630,8 @@ cp_print_static_field (struct type *type,
       return;
     }
 
-  if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
+  struct type *real_type = check_typedef (type);
+  if (TYPE_CODE (real_type) == TYPE_CODE_STRUCT)
     {
       CORE_ADDR *first_dont_print;
       CORE_ADDR addr;
@@ -668,7 +656,6 @@ cp_print_static_field (struct type *type,
       addr = value_address (val);
       obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
                    sizeof (CORE_ADDR));
-      type = check_typedef (type);
       cp_print_value_fields (type, value_enclosing_type (val),
                             value_embedded_offset (val), addr,
                             stream, recurse, val,
@@ -676,7 +663,7 @@ cp_print_static_field (struct type *type,
       return;
     }
 
-  if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
+  if (TYPE_CODE (real_type) == TYPE_CODE_ARRAY)
     {
       struct type **first_dont_print;
       int i;
@@ -759,7 +746,7 @@ cp_find_class_member (struct type **self_p, int *fieldno,
 
 void
 cp_print_class_member (const gdb_byte *valaddr, struct type *type,
-                      struct ui_file *stream, char *prefix)
+                      struct ui_file *stream, const char *prefix)
 {
   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
 
@@ -797,7 +784,7 @@ cp_print_class_member (const gdb_byte *valaddr, struct type *type,
       const char *name;
 
       fputs_filtered (prefix, stream);
-      name = type_name_no_tag (self_type);
+      name = TYPE_NAME (self_type);
       if (name)
        fputs_filtered (name, stream);
       else