[binutils-gdb] gdb: Check for not allocated/associated values during array slicing

Andrew Burgess aburgess@sourceware.org
Mon Jun 10 22:06:00 GMT 2019


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

commit a7067863418451b27130bb4300ac1890ff806c12
Author: Andrew Burgess <andrew.burgess@embecosm.com>
Date:   Thu May 23 19:49:41 2019 +0100

    gdb: Check for not allocated/associated values during array slicing
    
    When extracting an array slice we should give up if the array is
    not-allocated or not-associated.  For Fortran, at least in gfortran
    compiled code, the upper and lower bounds are undefined if the array
    is not allocated or not associated, in which case performing checks
    against these bounds will result in undefined behaviour.
    
    Better then to throw an error if we try to slice such an array.  This
    changes the error message that the user will receive in these
    cases (if they got an error message before).  Previously they may have
    gotten "slice out of range" now they'll get "array not allocated" or
    "array not associated".
    
    gdb/ChangeLog:
    
    	* valops.c (value_slice): Check for not allocated or not
    	associated values.
    
    gdb/testsuite/ChangeLog:
    
    	* gdb.fortran/vla-sizeof.exp: Update expected results.

Diff:
---
 gdb/ChangeLog                            | 5 +++++
 gdb/testsuite/ChangeLog                  | 4 ++++
 gdb/testsuite/gdb.fortran/vla-sizeof.exp | 4 ++--
 gdb/valops.c                             | 5 +++++
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 6f6e6ba..022ce04 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2019-06-10  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* valops.c (value_slice): Check for not allocated or not
+	associated values.
+
 2019-06-10  Tom de Vries  <tdevries@suse.de>
 
 	PR gdb/24618
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 7cc712b..33ae2f4 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2019-06-10  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+	* gdb.fortran/vla-sizeof.exp: Update expected results.
+
 2019-06-06  Amos Bird  <amosbird@gmail.com>
 
 	* gdb.base/annota1.exp (thread_switch): Add test for
diff --git a/gdb/testsuite/gdb.fortran/vla-sizeof.exp b/gdb/testsuite/gdb.fortran/vla-sizeof.exp
index b6fdaeb..4fe6938 100644
--- a/gdb/testsuite/gdb.fortran/vla-sizeof.exp
+++ b/gdb/testsuite/gdb.fortran/vla-sizeof.exp
@@ -32,7 +32,7 @@ gdb_test "print sizeof(vla1)" " = 0" "print sizeof non-allocated vla1"
 gdb_test "print sizeof(vla1(3,2,1))" \
     "no such vector element \\(vector not allocated\\)" \
     "print sizeof non-allocated indexed vla1"
-gdb_test "print sizeof(vla1(3:4,2,1))" "slice out of range" \
+gdb_test "print sizeof(vla1(3:4,2,1))" "array not allocated" \
     "print sizeof non-allocated sliced vla1"
 
 # Try to access value in allocated VLA
@@ -49,7 +49,7 @@ gdb_test "print sizeof(pvla)" " = 0" "print sizeof non-associated pvla"
 gdb_test "print sizeof(pvla(3,2,1))" \
     "no such vector element \\(vector not associated\\)" \
     "print sizeof non-associated indexed pvla"
-gdb_test "print sizeof(pvla(3:4,2,1))" "slice out of range" \
+gdb_test "print sizeof(pvla(3:4,2,1))" "array not associated" \
     "print sizeof non-associated sliced pvla"
 
 # Try to access values in pointer to VLA and compare them
diff --git a/gdb/valops.c b/gdb/valops.c
index fd92a4d..cbf2ecc 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -3801,6 +3801,11 @@ value_slice (struct value *array, int lowbound, int length)
       && TYPE_CODE (array_type) != TYPE_CODE_STRING)
     error (_("cannot take slice of non-array"));
 
+  if (type_not_allocated (array_type))
+    error (_("array not allocated"));
+  if (type_not_associated (array_type))
+    error (_("array not associated"));
+
   range_type = TYPE_INDEX_TYPE (array_type);
   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
     error (_("slice from bad array or bitstring"));



More information about the Gdb-cvs mailing list