]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add check for empty array
authorPiotr Rudnicki <piotr.rudnicki@intel.com>
Wed, 9 Apr 2025 13:17:31 +0000 (15:17 +0200)
committerPiotr Rudnicki <piotr.rudnicki@intel.com>
Mon, 14 Apr 2025 09:42:54 +0000 (11:42 +0200)
With the command before the change, gdb crashes with message:
(gdb) p 1 == { }
Fatal signal: Segmentation fault

After the fix in this commit, gdb shows following message:
(gdb) p 1 == { }
size of the array element must not be zero

Add new test cases to file gdb.base/printcmds.exp to test this change

Approved-By: Tom Tromey <tom@tromey.com>
gdb/testsuite/gdb.base/printcmds.exp
gdb/valops.c

index e1c996e99e16b792e7a899820cd2433e02a5d185..8634668605a4aa74bddd726180571a87b03ff842 100644 (file)
@@ -744,6 +744,12 @@ proc test_print_char_arrays {} {
     gdb_test_no_output "set print address off" "address off char arrays"
 }
 
+proc test_print_arrays_negative {} {
+    # Check whether correct error messages are printed
+    gdb_test "p 1 == { }" "size of the array element must not be zero"
+    gdb_test "p 1 == { 1, 'a' }" "array elements must all be the same size"
+}
+
 proc test_print_nibbles {} {
     gdb_test_no_output "set print nibbles on"
     foreach lang_line {
@@ -1235,6 +1241,7 @@ test_print_int_arrays
 test_print_typedef_arrays
 test_artificial_arrays
 test_print_char_arrays
+test_print_arrays_negative
 test_print_nibbles
 # We used to do the runto main here.
 test_print_string_constants
index 09af0ae99ec3e73375859b2623eb25d98b8b246f..1b63343c4a77d0ee17c8fa44e230e33b56932e05 100644 (file)
@@ -1695,6 +1695,9 @@ value_array (int lowbound, gdb::array_view<struct value *> elemvec)
   /* Validate that the bounds are reasonable and that each of the
      elements have the same size.  */
 
+  if (elemvec.empty ())
+    error (_("size of the array element must not be zero"));
+
   typelength = type_length_units (elemvec[0]->enclosing_type ());
   for (struct value *other : elemvec.slice (1))
     {