This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH] Fix float to LONGEST conversion.
- From: Andrew Burgess <andrew dot burgess at embecosm dot com>
- To: Ali Tamur <tamur at google dot com>
- Cc: gdb-patches at sourceware dot org
- Date: Wed, 28 Aug 2019 09:29:35 +0100
- Subject: Re: [PATCH] Fix float to LONGEST conversion.
- References: <20190828032808.242363-1-tamur@google.com>
* Ali Tamur via gdb-patches <gdb-patches@sourceware.org> [2019-08-27 20:28:08 -0700]:
> The code used to have undefined behaviour.
I would much prefer to see a more detailed explanation for _why_ the
previous behaviour is undefined. I ran my eye over the old code but
didn't see anything obvious.
If at a later date I come back and want to figure out why this patch
went in it would be nice if the commit message could tell me
everything I need to know.
Thanks,
Andrew
>
>
> gdb/ChangeLog:
> *gdb/target-float.c (host_float_ops<T>::to_longest): Update
> implementation.
> ---
> gdb/target-float.c | 17 +++++++++++------
> 1 file changed, 11 insertions(+), 6 deletions(-)
>
> diff --git a/gdb/target-float.c b/gdb/target-float.c
> index 39abb12696..0fd71c0dc3 100644
> --- a/gdb/target-float.c
> +++ b/gdb/target-float.c
> @@ -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
> --
> 2.23.0.187.g17f5b7556c-goog
>