Skip to content

Commit 18bc5b2

Browse files
[libc++] Fix ubsan error in __ct_abs
UBsan reports runtime error: negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself Fix this by performing the suggested casts.
1 parent 9ef2ac3 commit 18bc5b2

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

libcxx/include/__numeric/gcd_lcm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct __ct_abs<_Result, _Source, true> {
3939
if (__t >= 0)
4040
return __t;
4141
if (__t == numeric_limits<_Source>::min())
42-
return -static_cast<_Result>(__t);
42+
return static_cast<_Result>(-static_cast<std::make_unsigned_t<_Result>>(__t));
4343
return -__t;
4444
}
4545
};

0 commit comments

Comments
 (0)