[v3,3/7] Hoist array bounds check in array_operation::evaluate

Message ID 20230829-cleanup-array-op-v3-3-0d0256c6a6ed@adacore.com
State New
Headers
Series Small cleanups in array_operation::evaluate |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 fail Patch failed to apply

Commit Message

Tom Tromey Aug. 29, 2023, 6:10 p.m. UTC
  This hoists the array bounds check in array_operation::evaluate to
before the loop.

Reviewed-by: John Baldwin <jhb@FreeBSD.org>
---
 gdb/eval.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gdb/eval.c b/gdb/eval.c
index 63c414e546e..6cf72545f62 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -2426,6 +2426,8 @@  array_operation::evaluate (struct type *expect_type,
 	  low_bound = 0;
 	  high_bound = (type->length () / element_size) - 1;
 	}
+      if (low_bound + nargs - 1 > high_bound)
+	error (_("Too many array elements"));
       index = low_bound;
       memset (array->contents_raw ().data (), 0, expect_type->length ());
       for (int tem = 0; tem < nargs; ++tem)
@@ -2436,9 +2438,6 @@  array_operation::evaluate (struct type *expect_type,
 							  exp, noside);
 	  if (element->type () != element_type)
 	    element = value_cast (element_type, element);
-	  if (index > high_bound)
-	    /* To avoid memory corruption.  */
-	    error (_("Too many array elements"));
 	  memcpy (array->contents_raw ().data ()
 		  + (index - low_bound) * element_size,
 		  element->contents ().data (),