Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH][gdb/exp] Fix assert when adding ptr to imaginary unit
@ 2021-01-28 13:06 Tom de Vries
  2021-01-28 14:08 ` Tom Tromey
  0 siblings, 1 reply; 6+ messages in thread
From: Tom de Vries @ 2021-01-28 13:06 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Hi,

I'm running into this assertion failure:
...
$ gdb -batch -ex "p (void *)0 - 5i"
gdbtypes.c:3430: internal-error: \
  type* init_complex_type(const char*,   type*): Assertion \
  `target_type->code () == TYPE_CODE_INT \
   || target_type->code () == TYPE_CODE_FLT' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
...

This is a regression since commit c34e8714662 "Implement complex arithmetic".
Before that commit we had:
...
(gdb) p (void *)0 - 5i
Argument to arithmetic operation not a number or boolean.
...

Fix this in scalar_binop by further restricting what operands we allow to
complex_binop.

Tested on x86_64-linux.

Any comments?

Thanks,
- Tom

[gdb/exp] Fix assert when adding ptr to imaginary unit

gdb/ChangeLog:

2021-01-28  Tom de Vries  <tdevries@suse.de>

	PR exp/27265
	* valarith.c (scalar_binop): Don't call complex_binop unless
	arguments are either complex or can be casted to complex.

gdb/testsuite/ChangeLog:

2021-01-28  Tom de Vries  <tdevries@suse.de>

	PR exp/27265
	* gdb.base/complex-parts.exp: Add tests.

---
 gdb/testsuite/gdb.base/complex-parts.exp |  4 ++++
 gdb/valarith.c                           | 10 ++++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/gdb/testsuite/gdb.base/complex-parts.exp b/gdb/testsuite/gdb.base/complex-parts.exp
index 3677c05aa1d..db193934214 100644
--- a/gdb/testsuite/gdb.base/complex-parts.exp
+++ b/gdb/testsuite/gdb.base/complex-parts.exp
@@ -103,3 +103,7 @@ gdb_test "print (_Complex int) 4" " = 4 \\+ 0i"
 gdb_test "print (_Complex float) 4.5" " = 4.5 \\+ 0i"
 gdb_test "ptype __complex__ short" " = _Complex short"
 gdb_test "print (_Complex int) (23.75 + 8.88i)" " = 23 \\+ 8i"
+
+set re_reject_arg "Argument to arithmetic operation not a number or boolean\\."
+gdb_test "print (void *)0 + 5i" $re_reject_arg
+gdb_test "print (_Decimal32)0 + 5i" $re_reject_arg
diff --git a/gdb/valarith.c b/gdb/valarith.c
index 81d48aae82a..9e9e4db1cd5 100644
--- a/gdb/valarith.c
+++ b/gdb/valarith.c
@@ -1171,8 +1171,14 @@ scalar_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
   type1 = check_typedef (value_type (arg1));
   type2 = check_typedef (value_type (arg2));
 
-  if (type1->code () == TYPE_CODE_COMPLEX
-      || type2->code () == TYPE_CODE_COMPLEX)
+  if ((type1->code () == TYPE_CODE_COMPLEX
+       && (type2->code () == TYPE_CODE_COMPLEX
+	   || type2->code () == TYPE_CODE_INT
+	   || type2->code () == TYPE_CODE_FLT))
+      || ((type2->code () == TYPE_CODE_COMPLEX
+	   && (type1->code () == TYPE_CODE_COMPLEX
+	       || type1->code () == TYPE_CODE_INT
+	       || type1->code () == TYPE_CODE_FLT))))
     return complex_binop (arg1, arg2, op);
 
   if ((!is_floating_value (arg1)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-02-05 10:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-28 13:06 [PATCH][gdb/exp] Fix assert when adding ptr to imaginary unit Tom de Vries
2021-01-28 14:08 ` Tom Tromey
2021-01-29  7:04   ` Tom de Vries
2021-01-29  7:10     ` Tom de Vries
2021-02-05 10:05       ` Tom de Vries
2021-02-05  9:57     ` Tom de Vries

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox