You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The 64-bit, non-NaN-boxed variant of JS_NewFloat64 tries to check whether the input value d can be represented as a signed 32-bit integer by comparing the bit representation of a double before and after a round trip through an int32_t variable:
u.d = d;
val = (int32_t)d;
t.d = val;
Unfortunately, casting a double value to an integer type that can't hold it is undefined behavior, if I'm reading C17 section 6.3.1.4 right:
When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.
On 64-bit RISC-V, FreeBSD 13.0's Clang 11.0.1 in -O0 mode compiles this snippet to
The 64-bit, non-NaN-boxed variant of
JS_NewFloat64
tries to check whether the input valued
can be represented as a signed 32-bit integer by comparing the bit representation of a double before and after a round trip through anint32_t
variable:Unfortunately, casting a double value to an integer type that can't hold it is undefined behavior, if I'm reading C17 section 6.3.1.4 right:
On 64-bit RISC-V, FreeBSD 13.0's Clang 11.0.1 in
-O0
mode compiles this snippet towhere the sign extension performed by
lw
acts as a cast toint32_t
, as intended. By contrast, Clang-O2
avoids the memory accesses, producingThis means that the bit-representation test will catch negative 0 but not values in
int64_t
but outsideint32_t
range.A simple test case:
If I patch
JS_NewFloat64
to add comparisons againstINT32_MIN
andINT32_MAX
the effect goes away and the built-in test suite passes.The text was updated successfully, but these errors were encountered: