public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
* [binutils-gdb] [gdb/symtab] Make comp_unit_head.length private
@ 2022-12-30 12:55 Tom de Vries
  0 siblings, 0 replies; only message in thread
From: Tom de Vries @ 2022-12-30 12:55 UTC (permalink / raw)
  To: gdb-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=d8f52a9a9ccbf7411cf4ae487d2756826f5d0bd5

commit d8f52a9a9ccbf7411cf4ae487d2756826f5d0bd5
Author: Tom de Vries <tdevries@suse.de>
Date:   Fri Dec 30 13:55:22 2022 +0100

    [gdb/symtab] Make comp_unit_head.length private
    
    Make comp_unit_head.length private, to enforce using accessor functions.
    
    Replace accessor function get_length with get_length_with_initial and
    get_length_without_initial, to make it explicit which variant we're using.
    
    Tested on x86_64-linux.
    
    PR symtab/29343
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29343

Diff:
---
 gdb/dwarf2/comp-unit-head.c |  6 +++---
 gdb/dwarf2/comp-unit-head.h | 25 ++++++++++++++++++++-----
 gdb/dwarf2/read.c           | 20 ++++++++++----------
 3 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/gdb/dwarf2/comp-unit-head.c b/gdb/dwarf2/comp-unit-head.c
index edb2a8aa55a..b618a674808 100644
--- a/gdb/dwarf2/comp-unit-head.c
+++ b/gdb/dwarf2/comp-unit-head.c
@@ -44,7 +44,7 @@ read_comp_unit_head (struct comp_unit_head *cu_header,
   const char *filename = section->get_file_name ();
   bfd *abfd = section->get_bfd_owner ();
 
-  cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
+  cu_header->set_length (read_initial_length (abfd, info_ptr, &bytes_read));
   cu_header->initial_length_size = bytes_read;
   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
   info_ptr += bytes_read;
@@ -162,11 +162,11 @@ error_check_comp_unit_head (dwarf2_per_objfile *per_objfile,
 
   /* Cast to ULONGEST to use 64-bit arithmetic when possible to
      avoid potential 32-bit overflow.  */
-  if (((ULONGEST) header->sect_off + header->get_length ())
+  if (((ULONGEST) header->sect_off + header->get_length_with_initial ())
       > section->size)
     error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
 	   "(offset %s + 0) [in module %s]"),
-	   header->length, sect_offset_str (header->sect_off),
+	   header->get_length_without_initial (), sect_offset_str (header->sect_off),
 	   filename);
 }
 
diff --git a/gdb/dwarf2/comp-unit-head.h b/gdb/dwarf2/comp-unit-head.h
index 7579fe7fa29..45384fac720 100644
--- a/gdb/dwarf2/comp-unit-head.h
+++ b/gdb/dwarf2/comp-unit-head.h
@@ -34,7 +34,9 @@
    translation, looks like this.  */
 struct comp_unit_head
 {
-  unsigned int length = 0;
+private:
+  unsigned int m_length = 0;
+public:
   unsigned char version = 0;
   unsigned char addr_size = 0;
   unsigned char signed_addr_p = 0;
@@ -65,17 +67,30 @@ struct comp_unit_head
      DW_UT_skeleton or DW_UT_split_compile.  */
   ULONGEST signature = 0;
 
-  /* Return the total length of the CU described by this header.  */
-  unsigned int get_length () const
+  void set_length (unsigned int length)
   {
-    return initial_length_size + length;
+    m_length = length;
+  }
+
+  /* Return the total length of the CU described by this header, including the
+     initial length field.  */
+  unsigned int get_length_with_initial () const
+  {
+    return m_length + initial_length_size;
+  }
+
+  /* Return the total length of the CU described by this header, excluding the
+     initial length field.  */
+  unsigned int get_length_without_initial () const
+  {
+    return m_length;
   }
 
   /* Return TRUE if OFF is within this CU.  */
   bool offset_in_cu_p (sect_offset off) const
   {
     sect_offset bottom = sect_off;
-    sect_offset top = sect_off + get_length ();
+    sect_offset top = sect_off + get_length_with_initial ();
     return off >= bottom && off < top;
   }
 
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 28fadb886aa..b33ea6847e0 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -5598,7 +5598,7 @@ create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
       ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
 					   abbrev_section, ptr, section_kind);
 
-      length = header.get_length ();
+      length = header.get_length_with_initial ();
 
       /* Skip dummy type units.  */
       if (ptr >= info_ptr + length
@@ -6005,7 +6005,7 @@ read_cutu_die_from_dwo (dwarf2_cu *cu,
       gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
       /* For DWOs coming from DWP files, we don't know the CU length
 	 nor the type's offset in the TU until now.  */
-      dwo_unit->length = cu->header.get_length ();
+      dwo_unit->length = cu->header.get_length_with_initial ();
       dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
 
       /* Establish the type offset that can be used to lookup the type.
@@ -6021,7 +6021,7 @@ read_cutu_die_from_dwo (dwarf2_cu *cu,
       gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
       /* For DWOs coming from DWP files, we don't know the CU length
 	 until now.  */
-      dwo_unit->length = cu->header.get_length ();
+      dwo_unit->length = cu->header.get_length_with_initial ();
     }
 
   dwo_abbrev_section->read (objfile);
@@ -6289,7 +6289,7 @@ cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
 
 	  /* LENGTH has not been set yet for type units if we're
 	     using .gdb_index.  */
-	  this_cu->set_length (cu->header.get_length ());
+	  this_cu->set_length (cu->header.get_length_with_initial ());
 
 	  /* Establish the type offset that can be used to lookup the type.  */
 	  sig_type->type_offset_in_section =
@@ -6305,7 +6305,7 @@ cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
 						    rcuh_kind::COMPILE);
 
 	  gdb_assert (this_cu->sect_off == cu->header.sect_off);
-	  this_cu->set_length (cu->header.get_length ());
+	  this_cu->set_length (cu->header.get_length_with_initial ());
 	  this_cu->set_version (cu->header.version);
 	}
     }
@@ -6465,7 +6465,7 @@ cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
       m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
       m_new_cu->addr_base = parent_cu->addr_base;
     }
-  this_cu->set_length (m_new_cu->header.get_length ());
+  this_cu->set_length (m_new_cu->header.get_length_with_initial ());
 
   /* Skip dummy compilation units.  */
   if (info_ptr >= begin_info_ptr + this_cu->length ()
@@ -7249,7 +7249,7 @@ read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
 	  *slot = sig_ptr;
 	}
       this_cu->sect_off = sect_off;
-      this_cu->set_length (cu_header.get_length ());
+      this_cu->set_length (cu_header.get_length_with_initial ());
       this_cu->is_dwz = is_dwz;
       this_cu->section = section;
       /* Init this asap, to avoid a data race in the set_version in
@@ -7744,7 +7744,7 @@ load_full_comp_unit (dwarf2_per_cu_data *this_cu,
 
   gdb_assert (cu->die_hash == NULL);
   cu->die_hash =
-    htab_create_alloc_ex (cu->header.length / 12,
+    htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
 			  die_hash,
 			  die_eq,
 			  NULL,
@@ -18409,7 +18409,7 @@ cooked_indexer::index_dies (cutu_reader *reader,
 {
   const gdb_byte *end_ptr = (reader->buffer
 			     + to_underlying (reader->cu->header.sect_off)
-			     + reader->cu->header.get_length ());
+			     + reader->cu->header.get_length_with_initial ());
 
   while (info_ptr < end_ptr)
     {
@@ -23026,7 +23026,7 @@ read_signatured_type (signatured_type *sig_type,
 
       gdb_assert (cu->die_hash == NULL);
       cu->die_hash =
-	htab_create_alloc_ex (cu->header.length / 12,
+	htab_create_alloc_ex (cu->header.get_length_without_initial () / 12,
 			      die_hash,
 			      die_eq,
 			      NULL,

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-12-30 12:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-30 12:55 [binutils-gdb] [gdb/symtab] Make comp_unit_head.length private Tom de Vries

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