Assert that a floating type's length is at least as long as its format
authorPedro Alves <palves@redhat.com>
Wed, 9 Mar 2016 02:29:39 +0000 (02:29 +0000)
committerPedro Alves <palves@redhat.com>
Wed, 9 Mar 2016 02:47:40 +0000 (02:47 +0000)
This would have caught the HP/PA bug fixed in the previous patch:

 .../src/gdb/gdbtypes.c:4690: internal-error: arch_float_type: Assertion `len >= floatformat_totalsize_bytes (floatformats[0])' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n)

Tested on x86-64 Fedora 23, --enable-targets=all.

gdb/ChangeLog:
2016-03-09  Pedro Alves  <palves@redhat.com>

* doublest.c (floatformat_totalsize_bytes): New function.
(floatformat_from_type): Assert that the type's length is at least
as long as the floatformat's totalsize.
* doublest.h (floatformat_totalsize_bytes): New declaration.
* gdbtypes.c (arch_float_type): Assert that the type's length is
at least as long as the floatformat's totalsize.

gdb/ChangeLog
gdb/doublest.c
gdb/doublest.h
gdb/gdbtypes.c

index 7303c32488ab4b5f5f28693618211838ea94bb27..abb224d548fced7ca5009948086aada22ef5d988 100644 (file)
@@ -1,3 +1,12 @@
+2016-03-09  Pedro Alves  <palves@redhat.com>
+
+       * doublest.c (floatformat_totalsize_bytes): New function.
+       (floatformat_from_type): Assert that the type's length is at least
+       as long as the floatformat's totalsize.
+       * doublest.h (floatformat_totalsize_bytes): New declaration.
+       * gdbtypes.c (arch_float_type): Assert that the type's length is
+       at least as long as the floatformat's totalsize.
+
 2016-03-09  Pedro Alves  <palves@redhat.com>
 
        * hppa-linux-tdep.c (hppa_linux_init_abi): Set the long double
index 026cf42ef523f0abc51c6368211e5d9f875c6836..fe06fec01e399b61dd2970a21529fcbee05f76ad 100644 (file)
@@ -693,6 +693,15 @@ static const struct floatformat *host_double_format = GDB_HOST_DOUBLE_FORMAT;
 static const struct floatformat *host_long_double_format
   = GDB_HOST_LONG_DOUBLE_FORMAT;
 
+/* See doublest.h.  */
+
+size_t
+floatformat_totalsize_bytes (const struct floatformat *fmt)
+{
+  return ((fmt->totalsize + FLOATFORMAT_CHAR_BIT - 1)
+         / FLOATFORMAT_CHAR_BIT);
+}
+
 void
 floatformat_to_doublest (const struct floatformat *fmt,
                         const void *in, DOUBLEST *out)
@@ -802,12 +811,16 @@ const struct floatformat *
 floatformat_from_type (const struct type *type)
 {
   struct gdbarch *gdbarch = get_type_arch (type);
+  const struct floatformat *fmt;
 
   gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
   if (TYPE_FLOATFORMAT (type) != NULL)
-    return TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)];
+    fmt = TYPE_FLOATFORMAT (type)[gdbarch_byte_order (gdbarch)];
   else
-    return floatformat_from_length (gdbarch, TYPE_LENGTH (type));
+    fmt = floatformat_from_length (gdbarch, TYPE_LENGTH (type));
+
+  gdb_assert (TYPE_LENGTH (type) >= floatformat_totalsize_bytes (fmt));
+  return fmt;
 }
 
 /* Extract a floating-point number of type TYPE from a target-order
index ae42bdb1bdfe6f18e1de75466a28b141b1628c25..9522829bc3440c72ec0d8288be83d0245c556590 100644 (file)
@@ -89,6 +89,10 @@ extern const char *floatformat_mantissa (const struct floatformat *,
 
 const struct floatformat *floatformat_from_type (const struct type *type);
 
+/* Return the floatformat's total size in host bytes.  */
+
+extern size_t floatformat_totalsize_bytes (const struct floatformat *fmt);
+
 extern DOUBLEST extract_typed_floating (const void *addr,
                                        const struct type *type);
 extern void store_typed_floating (void *addr, const struct type *type,
index f129b0e3cadd14e66ef649013379aa475394927a..1af6fff67f48b00cc2ceb1ed4176c1bb65f07a00 100644 (file)
@@ -4682,6 +4682,15 @@ arch_float_type (struct gdbarch *gdbarch,
 
   t = arch_type (gdbarch, TYPE_CODE_FLT, bit / TARGET_CHAR_BIT, name);
   TYPE_FLOATFORMAT (t) = floatformats;
+
+  if (floatformats != NULL)
+    {
+      size_t len = TYPE_LENGTH (t);
+
+      gdb_assert (len >= floatformat_totalsize_bytes (floatformats[0]));
+      gdb_assert (len >= floatformat_totalsize_bytes (floatformats[1]));
+    }
+
   return t;
 }
 
This page took 0.029427 seconds and 4 git commands to generate.