[v3,1/1] gdb: add check for empty array

Message ID 20250409140547.244372-1-piotr.rudnicki@intel.com
State New
Headers
Series [v3,1/1] gdb: add check for empty array |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed

Commit Message

Piotr Rudnicki April 9, 2025, 2:05 p.m. UTC
  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
---
 gdb/testsuite/gdb.base/printcmds.exp | 7 +++++++
 gdb/valops.c                         | 3 +++
 2 files changed, 10 insertions(+)
  

Comments

Tom Tromey April 10, 2025, 3:17 p.m. UTC | #1
>>>>> "Piotr" == Piotr Rudnicki <piotr.rudnicki@intel.com> writes:

Piotr> With the command before the change, gdb crashes with message:
Piotr> (gdb) p 1 == { }
Piotr> Fatal signal: Segmentation fault

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

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

Thanks, this is ok.

Approved-By: Tom Tromey <tom@tromey.com>

Do you have write-after-approval access?

If so you should first send an "FYI" patch to add yourself to the
appropriate spot in gdb/MAINTAINERS.  Then after that is in you can push
approved patches.

Tom
  

Patch

diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index e1c996e99e1..8634668605a 100644
--- a/gdb/testsuite/gdb.base/printcmds.exp
+++ b/gdb/testsuite/gdb.base/printcmds.exp
@@ -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
diff --git a/gdb/valops.c b/gdb/valops.c
index 09af0ae99ec..1b63343c4a7 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -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))
     {