Fix float to LONGEST conversion.
authorAli Tamur <tamur@google.com>
Wed, 28 Aug 2019 01:58:57 +0000 (18:58 -0700)
committerAli Tamur <tamur@google.com>
Wed, 11 Sep 2019 19:21:50 +0000 (12:21 -0700)
The code used to have undefined behaviour when template parameter is float and
host_float is NaN, because it attempted to convert NaN value to LONGEST at the
last statement. This frequently caused crashes on tests that checked "info
all-registers" (at least when the code is compiled with clang; I didn't test
with gdb).

gdb/ChangeLog:

*target-float.c (host_float_ops<T>::to_longest): Update
implementation.

gdb/ChangeLog
gdb/target-float.c

index 3b28884267b30a3c22c4991949b1951e88e83ae4..eccd4ef2bdd79da07c56cd34c2ea3f4216ecf4a8 100644 (file)
@@ -1,3 +1,8 @@
+2019-09-11  Ali Tamur  <tamur@google.com>
+
+       *gdb/target-float.c (host_float_ops<T>::to_longest): Update
+       implementation.
+
 2019-09-11  Christian Biesinger  <cbiesinger@google.com>
 
        * dbxread.c (read_dbx_symtab): Update.
index 39abb126965ea47e144cf79110c5837674f400ed..0fd71c0dc335f0e6b0fa76a7e8ad24187d97bf79 100644 (file)
@@ -1007,13 +1007,18 @@ host_float_ops<T>::to_longest (const gdb_byte *addr,
 {
   T host_float;
   from_target (type, addr, &host_float);
-  /* Converting an out-of-range value is undefined behavior in C, but we
-     prefer to return a defined value here.  */
-  if (host_float > std::numeric_limits<LONGEST>::max())
-    return std::numeric_limits<LONGEST>::max();
-  if (host_float < std::numeric_limits<LONGEST>::min())
+  T min_possible_range = static_cast<T>(std::numeric_limits<LONGEST>::min());
+  T max_possible_range = -min_possible_range;
+  /* host_float can be converted to an integer as long as it's in
+     the range [min_possible_range, max_possible_range). If not, it is either
+     too large, or too small, or is NaN; in this case return the maximum or
+     minimum possible value.  */
+  if (host_float < max_possible_range && host_float >= min_possible_range)
+    return static_cast<LONGEST> (host_float);
+  if (host_float < min_possible_range)
     return std::numeric_limits<LONGEST>::min();
-  return (LONGEST) host_float;
+  /* This line will be executed if host_float is NaN.  */
+  return std::numeric_limits<LONGEST>::max();
 }
 
 /* Convert signed integer VAL to a target floating-number of type TYPE
This page took 0.035434 seconds and 4 git commands to generate.