]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
pow,powf(3),__ieee754_rem_pio2(f): Avoid negative integer left shift UB
authorKarl Tomlinson <karlt+@karlt.net>
Tue, 23 Apr 2024 18:18:25 +0000 (12:18 -0600)
committerWarner Losh <imp@FreeBSD.org>
Tue, 23 Apr 2024 20:04:07 +0000 (14:04 -0600)
commite75a1bbc233bf112b9eb98a20ad4bdf9bc14b2cf
treefe718338fc1a4441d593e08dd09228b918e3713e
parentaa66995b4c804cbb579f71645d97fe282a798bfc
pow,powf(3),__ieee754_rem_pio2(f): Avoid negative integer left shift UB

A compiler clever enough to know that z is positive with a non-zero
biased exponent could, for example, optimize away the scalbnf(z,n) in
pow() because behavior for left shift of negative values is undefined.
`n` is negative when y*log2(|x|) < -0.5.  i.e. |x^y| < sqrt(0.5)

The intended behavior for operator<< in this code is to shift the two's
complement representation of the first operand.

In the pow() functions, the result is added to the IEEE 754 exponent of
z = 2^y'.  n may be negative enough to underflow the biased IEEE 754
exponent below zero, which is manifested in the sign bit of j
(which would correspond to the IEEE 754 sign bit).

The conversion from uint32_t to int32_t for out-of-int32_t-range values
is implementation defined.  The assumed behavior of interpreting the
uint32_t value as a two's complement representation of a signed value
is already assumed in many parts of the code, such as uses of
GET_FLOAT_WORD() with signed integers.

This code passes all the current tests, and makes some out of tree
fuzzing tests pass again rather than hit UB (detailed in the commentary
of the pull request).

Signed-off-by: Karl Tomlinson <karlt+@karlt.net>
Reviewed by: imp, steve kargl, dim
Pull Request: https://github.com/freebsd/freebsd-src/pull/1137
lib/msun/src/e_pow.c
lib/msun/src/e_powf.c
lib/msun/src/e_rem_pio2.c
lib/msun/src/e_rem_pio2f.c