[PATCH] math: Fix errno setting for narrowing sqrt functions on underflow [BZ #33393]
Sam James
sam@gentoo.org
Sun Oct 19 16:11:09 GMT 2025
Osama Abdelkader <osama.abdelkader@gmail.com> writes:
> The CHECK_NARROW_SQRT macro in math-narrow.h was not setting errno to
> ERANGE when an underflow exception occurred during narrowing square root
> operations. While the FE_UNDERFLOW exception flag was correctly set, errno
> remained unchanged, violating IEEE 754 and POSIX requirements.
>
> This fix adds a check for the FE_UNDERFLOW exception after computing the
> result. When underflow is detected via fetestexcept(FE_UNDERFLOW), errno
> is now properly set to ERANGE.
>
> The issue affects all narrowing square root functions that use this macro:
> - fsqrt (double to float)
> - fsqrtl (long double to float)
> - dsqrtl (long double to double)
>
> Tested on x86_64-linux-gnu. All tests pass including the new test case
> that verifies errno is set correctly on underflow conditions.
What test case?
>
> Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
> ---
> math/math-narrow.h | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/math/math-narrow.h b/math/math-narrow.h
> index 2f27b736fd..89bbcc26f1 100644
> --- a/math/math-narrow.h
> +++ b/math/math-narrow.h
> @@ -307,6 +307,8 @@
> } \
> else if ((RET) == 0 && (X) != 0) \
> __set_errno (ERANGE); \
> + else if (fetestexcept (FE_UNDERFLOW)) \
> + __set_errno (ERANGE); \
> } \
> while (0)
More information about the Libc-alpha
mailing list