[PR,gdb/24789] Allow pointer arithmetic with integer references

Message ID 20200331133547.547-1-ssbssa@yahoo.de
State New
Headers show
Series
  • [PR,gdb/24789] Allow pointer arithmetic with integer references
Related show

Commit Message

Pedro Alves via Gdb-patches March 31, 2020, 1:35 p.m.
Considering these variables:
int i = 3;
int &iref = i;

It's not possible to do any pointer arithmetic with iref:
(gdb) p &i+iref
Argument to arithmetic operation not a number or boolean.

So this adds checks for references to integers in pointer arithmetic.

gdb/ChangeLog:

2020-03-31  Hannes Domani  <ssbssa@yahoo.de>

	PR gdb/24789
	* eval.c (evaluate_subexp_standard): Allow integer references
	in pointer arithmetic.
---
 gdb/eval.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

-- 
2.26.0

Comments

Tom Tromey March 31, 2020, 8:15 p.m. | #1
>>>>> "Hannes" == Hannes Domani via Gdb-patches <gdb-patches@sourceware.org> writes:


Thanks for the patch.

Hannes> So this adds checks for references to integers in pointer arithmetic.

This should have a test case.  It's fine to add it to some appropriate
pre-existing C++ test case.

Hannes> +	       && TYPE_IS_REFERENCE (type = check_typedef (value_type (arg2)))

Generally gdb doesn't use assignment in expressions; and in this case it
seems particularly bad to me.

Tom

Patch

diff --git a/gdb/eval.c b/gdb/eval.c
index 17af1b51df..aa629b7d02 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -2210,9 +2210,17 @@  evaluate_subexp_standard (struct type *expect_type,
       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
 	       && is_integral_type (value_type (arg2)))
 	return value_ptradd (arg1, value_as_long (arg2));
+      else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
+	       && TYPE_IS_REFERENCE (type = check_typedef (value_type (arg2)))
+	       && is_integral_type (TYPE_TARGET_TYPE (type)))
+	return value_ptradd (arg1, value_as_long (arg2));
       else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
 	       && is_integral_type (value_type (arg1)))
 	return value_ptradd (arg2, value_as_long (arg1));
+      else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
+	       && TYPE_IS_REFERENCE (type = check_typedef (value_type (arg1)))
+	       && is_integral_type (TYPE_TARGET_TYPE (type)))
+	return value_ptradd (arg2, value_as_long (arg1));
       else
 	{
 	  binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
@@ -2236,6 +2244,10 @@  evaluate_subexp_standard (struct type *expect_type,
       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
 	       && is_integral_type (value_type (arg2)))
 	return value_ptradd (arg1, - value_as_long (arg2));
+      else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
+	       && TYPE_IS_REFERENCE (type = check_typedef (value_type (arg2)))
+	       && is_integral_type (TYPE_TARGET_TYPE (type)))
+	return value_ptradd (arg1, - value_as_long (arg2));
       else
 	{
 	  binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);