[gdb/exp] Fix assert when adding ptr to imaginary unit
authorTom de Vries <tdevries@suse.de>
Fri, 5 Feb 2021 09:56:39 +0000 (10:56 +0100)
committerTom de Vries <tdevries@suse.de>
Fri, 5 Feb 2021 09:56:39 +0000 (10:56 +0100)
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 complex_binop by throwing an error, such that we have:
...
(gdb) print (void *)0 - 5i
Argument to complex arithmetic operation not supported.
...

Tested on x86_64-linux.

gdb/ChangeLog:

2021-02-05  Tom de Vries  <tdevries@suse.de>

PR exp/27265
* valarith.c (complex_binop): Throw an error if complex type can't
be created.

gdb/testsuite/ChangeLog:

2021-02-05  Tom de Vries  <tdevries@suse.de>

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

gdb/ChangeLog
gdb/gdbtypes.c
gdb/gdbtypes.h
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/complex-parts.exp
gdb/valarith.c

index 94b5c7ae12b9c87375f50edb881174467a31a968..07fe6a0184485c31538b6eb99b6d3b41b8f3a319 100644 (file)
@@ -1,3 +1,9 @@
+2021-02-05  Tom de Vries  <tdevries@suse.de>
+
+       PR exp/27265
+       * valarith.c (complex_binop): Throw an error if complex type can't
+       be created.
+
 2021-02-05  Tom de Vries  <tdevries@suse.de>
 
        PR symtab/27307
index 4dd1a6a64ec27590fefd49bd0f1a80fd2b9548d3..c736dff2ca82f9dd588ef1e30227308798bf6b07 100644 (file)
@@ -3413,6 +3413,15 @@ init_decfloat_type (struct objfile *objfile, int bit, const char *name)
   return t;
 }
 
+/* Return true if init_complex_type can be called with TARGET_TYPE.  */
+
+bool
+can_create_complex_type (struct type *target_type)
+{
+  return (target_type->code () == TYPE_CODE_INT
+         || target_type->code () == TYPE_CODE_FLT);
+}
+
 /* Allocate a TYPE_CODE_COMPLEX type structure.  NAME is the type
    name.  TARGET_TYPE is the component type.  */
 
@@ -3421,8 +3430,7 @@ init_complex_type (const char *name, struct type *target_type)
 {
   struct type *t;
 
-  gdb_assert (target_type->code () == TYPE_CODE_INT
-             || target_type->code () == TYPE_CODE_FLT);
+  gdb_assert (can_create_complex_type (target_type));
 
   if (TYPE_MAIN_TYPE (target_type)->flds_bnds.complex_type == nullptr)
     {
index 40b1aed031e70f303ff55377990fe6dbdc6fc4cf..45014a2b3e83c306ceea139d61edeaf01410c36d 100644 (file)
@@ -2289,6 +2289,7 @@ extern struct type *init_float_type (struct objfile *, int, const char *,
                                     const struct floatformat **,
                                     enum bfd_endian = BFD_ENDIAN_UNKNOWN);
 extern struct type *init_decfloat_type (struct objfile *, int, const char *);
+extern bool can_create_complex_type (struct type *);
 extern struct type *init_complex_type (const char *, struct type *);
 extern struct type *init_pointer_type (struct objfile *, int, const char *,
                                       struct type *);
index 01539a6224811706b61e1130c8e02ea86ac4ebca..9c24a49676af0a7daea8a8f6e82fc2e5c0e03c57 100644 (file)
@@ -1,3 +1,8 @@
+2021-02-05  Tom de Vries  <tdevries@suse.de>
+
+       PR exp/27265
+       * gdb.base/complex-parts.exp: Add tests.
+
 2021-02-05  Tom de Vries  <tdevries@suse.de>
 
        PR symtab/27307
index 3677c05aa1de6886b3dd9427066e760519d60f4c..6385752a2f004216c892491ce3d9560a1c917311 100644 (file)
@@ -103,3 +103,14 @@ 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 complex arithmetic operation not supported\\."
+gdb_test "print (void *)0 + 5i" $re_reject_arg
+gdb_test "print (_Decimal32)0 + 5i" $re_reject_arg
+
+# Set language to c++.  Avoid warning by not having current frame.
+clean_restart
+gdb_test_no_output "set language c++"
+
+# C++ type tests.
+gdb_test "print (bool)1 + 1i" " = 1 \\+ 1i"
index 315030988f45a755b861eac0a6f89f3fa57dec06..299a99f4703d0e7fd4ffa1c86888ca19a9c744ce 100644 (file)
@@ -1076,6 +1076,9 @@ complex_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
 
   struct type *comp_type = promotion_type (value_type (arg1_real),
                                           value_type (arg2_real));
+  if (!can_create_complex_type (comp_type))
+    error (_("Argument to complex arithmetic operation not supported."));
+
   arg1_real = value_cast (comp_type, arg1_real);
   arg1_imag = value_cast (comp_type, arg1_imag);
   arg2_real = value_cast (comp_type, arg2_real);
This page took 0.03869 seconds and 4 git commands to generate.