[binutils-gdb] Avoid crash in Ada value printing with optimized-out array

Tom Tromey tromey@sourceware.org
Thu Apr 15 16:25:45 GMT 2021


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

commit a15a276b46bf07323a1d270d7abece83ef1ea78f
Author: Tom Tromey <tromey@adacore.com>
Date:   Thu Apr 15 10:14:11 2021 -0600

    Avoid crash in Ada value printing with optimized-out array
    
    The Ada value-printing code could crash when printing an array which
    had been optimized out.
    
    The crash is difficult to reproduce, but I did manage to write a test
    that at least shows that the previous behavior was incorrect -- before
    the patch, the array is printed as if it is valid and every value is 0.
    
    gdb/ChangeLog
    2021-04-15  Tom Tromey  <tromey@adacore.com>
    
            * ada-valprint.c (ada_value_print_array): Handle optimized-out
            arrays.
    
    gdb/testsuite/ChangeLog
    2021-04-15  Tom Tromey  <tromey@adacore.com>
    
            * gdb.dwarf2/arr-stride.exp: Add test.

Diff:
---
 gdb/ChangeLog                           |  5 +++++
 gdb/ada-valprint.c                      |  5 ++++-
 gdb/testsuite/ChangeLog                 |  4 ++++
 gdb/testsuite/gdb.dwarf2/arr-stride.exp | 33 ++++++++++++++++++++++++++++++++-
 4 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 54a035c51bb..5b2f6c9d7c9 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2021-04-15  Tom Tromey  <tromey@adacore.com>
+
+	* ada-valprint.c (ada_value_print_array): Handle optimized-out
+	arrays.
+
 2021-04-15  Tom Tromey  <tromey@adacore.com>
 
 	* printcmd.c (print_variable_and_value): Use
diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c
index 0d5b6d73076..61c903bbed5 100644
--- a/gdb/ada-valprint.c
+++ b/gdb/ada-valprint.c
@@ -897,7 +897,10 @@ ada_value_print_array (struct value *val, struct ui_file *stream, int recurse,
 
   fprintf_filtered (stream, "(");
   print_optional_low_bound (stream, type, options);
-  if (TYPE_FIELD_BITSIZE (type, 0) > 0)
+
+  if (value_entirely_optimized_out (val))
+    val_print_optimized_out (val, stream);
+  else if (TYPE_FIELD_BITSIZE (type, 0) > 0)
     {
       const gdb_byte *valaddr = value_contents_for_printing (val);
       int offset_aligned = ada_aligned_value_addr (type, valaddr) - valaddr;
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index f58d771f3ec..d1b6f5c650f 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2021-04-15  Tom Tromey  <tromey@adacore.com>
+
+	* gdb.dwarf2/arr-stride.exp: Add test.
+
 2021-04-15  Andrew Burgess  <andrew.burgess@embecosm.com>
 
 	* gdb.base/startup-file.exp: Add more tests.
diff --git a/gdb/testsuite/gdb.dwarf2/arr-stride.exp b/gdb/testsuite/gdb.dwarf2/arr-stride.exp
index bf30f10c5c8..f25518f4558 100644
--- a/gdb/testsuite/gdb.dwarf2/arr-stride.exp
+++ b/gdb/testsuite/gdb.dwarf2/arr-stride.exp
@@ -31,7 +31,7 @@ Dwarf::assemble $asm_file {
                 {DW_AT_comp_dir /tmp}
         } {
 	    declare_labels integer_label array_elt_label array_label \
-                big_array_label
+                big_array_label struct_label
 
             integer_label: DW_TAG_base_type {
                 {DW_AT_byte_size 4 DW_FORM_sdata}
@@ -79,6 +79,34 @@ Dwarf::assemble $asm_file {
 		    {DW_AT_upper_bound 4 DW_FORM_data1}
 		}
 	    }
+
+	    struct_label: DW_TAG_structure_type {
+		{name struct_type}
+		{byte_size 16 DW_FORM_sdata}
+	    } {
+		member {
+		    {name intfield}
+		    {type :$integer_label}
+		    {data_member_location 0 DW_FORM_sdata}
+		}
+		member {
+		    {name arrayfield}
+		    {type :$array_label}
+		    {data_member_location 4 DW_FORM_sdata}
+		}
+	    }
+
+	    DW_TAG_variable {
+		{name the_struct}
+		{external 1 DW_FORM_flag}
+		{location {
+		    DW_OP_const1u 1
+		    DW_OP_stack_value
+		    DW_OP_piece 4
+		    DW_OP_piece 12
+		} SPECIAL_expr}
+		{type :$struct_label}
+	    }
 	}
     }
 }
@@ -95,3 +123,6 @@ gdb_test "ptype pck.table" \
 
 gdb_test "ptype pck.big_table" \
          "type = array \\(0 \\.\\. 4\\) of pck\\.item <packed: 8-bit elements>"
+
+gdb_test "print the_struct" \
+    "\\(intfield => 1, arrayfield => \\(0 => <optimized out>\\)\\)"


More information about the Gdb-cvs mailing list