Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CADDzAfMObuWsziV3PwSWMDhUHV8KsR8ebecEOiaMtZu=xT6X4g@mail.gmail.com>
Date: Tue, 13 Oct 2026 19:34:15 +0800
From: Kang-Che Sung <explorer09@...il.com>
To: musl@...ts.openwall.com
Cc: Song-Jiang <songjiang@...gxue.org>
Subject: Re: strtod/strtof: errno not set to ERANGE when result rounds
 up to a subnormal power of two

On Tue, Oct 13, 2026 at 5:11 AM Rich Felker <dalias@...c.org> wrote:
>
> On Mon, Oct 12, 2026 at 03:53:14PM +0000, Song-Jiang wrote:
> > musl's strtod and strtof do not set errno to ERANGE when an inexact
> > decimal input rounds *up* to a subnormal power of two. The returned
> > value is correct; only errno is missing. ERANGE is set for every other
> > underflowing input I tried: subnormal results that are not powers of
> > two, subnormal powers of two reached by rounding *down* (e.g. "5e-324"),
> > and inputs just below the smallest normal. Inexact inputs that round up
> > to the smallest normal itself (e.g. "2.2250738585072014e-308")
> > correctly stay silent, so this is not the usual FLT_MIN/DBL_MIN case.
>
> Attached patch should fix this.
>

By the way, what's the behavior of `strtod`/`strtof` if the result is
too small to represent even for a subnormal number and the rounding
mode forces the functions to round *down* the results to zero?

Note: The C standard does not require the result of an underflow to be
indicated by setting `errno` to `ERANGE`. The C standard says:
"[W]hether `errno` acquires the value `ERANGE` is implementation-defined".
So it's okay for a library to not set `errno` to `ERANGE` for the
underflow cases.

Personally I think using `errno` for catching an underflow is a bad
idea. First, C has API for retrieving the floating point exception
flags directly (`fetestexcept` and like). Seconds, sometime an
underflow error is indistinguishable from the generic `FE_INEXACT`
error, and the latter does not require setting `errno`. I think for
the intuitive use for floating points, it's better to not set `errno`
for any underflow case at all.

Just my opinion.

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.