diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp index e2b0eac70d..0317f35882 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.cpp +++ b/Userland/Libraries/LibJS/Runtime/Value.cpp @@ -637,7 +637,9 @@ u32 Value::to_u32(GlobalObject& global_object) const if (signbit(value)) int_val = -int_val; auto int32bit = fmod(int_val, NumericLimits::max() + 1.0); - return static_cast(int32bit); + // Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior + // Otherwise, negative numbers cause a UBSAN warning. + return static_cast(static_cast(int32bit)); } // 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16