]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb] Fix hard-coded constants in buildsym_compunit::make_blockvector
authorTom de Vries <tdevries@suse.de>
Fri, 17 Jul 2026 14:30:15 +0000 (16:30 +0200)
committerTom de Vries <tdevries@suse.de>
Fri, 17 Jul 2026 14:30:15 +0000 (16:30 +0200)
I came across some code in buildsym_compunit::make_blockvector that uses
hardcoded constants 0 and 1:
...
      gdb_assert (blockvector->block (0)->is_global_block ());
      gdb_assert (blockvector->block (1)->is_static_block ());
...

Fix this by instead using the symbolic constants GLOBAL_BLOCK and
STATIC_BLOCK.

The same function has an odd-looking for loop that uses a hard-coded '1' to
skip the global block:
...
       /* The 'J > 1' here is so that we don't place the global block into
   the map.  For CU with gaps, the static block will reflect the
   gaps, while the global block will just reflect the full extent of
   the range.  */
      for (int j = num_blocks; j > 1; )
  {
  --j;
    struct block *b = blockvector->block (j);
...

Fix this by rewriting it into an ordinary descending for loop, and using
symbolic constant GLOBAL_BLOCK to avoid the global block:
...
      for (int j = num_blocks - 1; j > GLOBAL_BLOCK; --j)
  {
    struct block *b = blockvector->block (j);
...

Approved-By: Tom Tromey <tom@tromey.com>
gdb/buildsym.c

index b543cd10eb546c3f3a3412879072d452bc1ba454..2b0a9a39b81a8ac6924175ce07cb2c794cfc3480 100644 (file)
@@ -363,16 +363,15 @@ buildsym_compunit::make_blockvector ()
       gdb_assert (num_blocks > 1);
 
       /* Assert our understanding of how the blocks are laid out.  */
-      gdb_assert (blockvector->block (0)->is_global_block ());
-      gdb_assert (blockvector->block (1)->is_static_block ());
+      gdb_assert (blockvector->block (GLOBAL_BLOCK)->is_global_block ());
+      gdb_assert (blockvector->block (STATIC_BLOCK)->is_static_block ());
 
-      /* The 'J > 1' here is so that we don't place the global block into
-        the map.  For CU with gaps, the static block will reflect the
-        gaps, while the global block will just reflect the full extent of
+      /* The 'J > GLOBAL_BLOCK' here is so that we don't place the global
+        block into the map.  For CU with gaps, the static block will reflect
+        the gaps, while the global block will just reflect the full extent of
         the range.  */
-      for (int j = num_blocks; j > 1; )
+      for (int j = num_blocks - 1; j > GLOBAL_BLOCK; --j)
        {
-         --j;
          struct block *b = blockvector->block (j);
 
          gdb_assert (!b->is_global_block ());