mirror of
https://gitlab.kitware.com/cmake/cmake.git
synced 2026-09-25 04:09:36 +03:00
math: Avoid signed integer overflow on negation
Extend commit 099c05dd04 (math: Avoid signed integer overflow when
intrinsics are available, 2026-02-26) to cover unary `-`.
This commit is contained in:
@@ -203,6 +203,10 @@ std::int64_t cmExprParserHelper::Mod(std::int64_t l, std::int64_t r)
|
||||
|
||||
std::int64_t cmExprParserHelper::Neg(std::int64_t x)
|
||||
{
|
||||
if (static_cast<std::uint64_t>(x) == 0x8000000000000000) {
|
||||
this->Warning(cmStrCat("signed integer cannot negate:\n ", x));
|
||||
return x;
|
||||
}
|
||||
return -x;
|
||||
}
|
||||
|
||||
|
||||
@@ -60,4 +60,12 @@ CMake Warning \(dev\) at [^
|
||||
|
||||
9223372036854775807 \* 2
|
||||
|
||||
This warning is for project developers\. Use -Wno-dev to suppress it\.
|
||||
|
||||
CMake Warning \(dev\) at [^
|
||||
]+/Tests/RunCMake/math/Overflow\.cmake:[0-9]+ \(math\):
|
||||
signed integer cannot negate:
|
||||
|
||||
-9223372036854775808
|
||||
|
||||
This warning is for project developers\. Use -Wno-dev to suppress it\.$
|
||||
|
||||
@@ -6,4 +6,5 @@
|
||||
-- 4 >> 65: 2
|
||||
-- 0x7FFFFFFFFFFFFFFF \+ 1: -9223372036854775808
|
||||
-- -0x7FFFFFFFFFFFFFFF - 2: 9223372036854775807
|
||||
-- 0x7FFFFFFFFFFFFFFF \* 2: -2$
|
||||
-- 0x7FFFFFFFFFFFFFFF \* 2: -2
|
||||
-- -~0x7FFFFFFFFFFFFFFF: -9223372036854775808$
|
||||
|
||||
@@ -8,6 +8,7 @@ foreach(expr IN ITEMS
|
||||
" 0x7FFFFFFFFFFFFFFF + 1"
|
||||
"-0x7FFFFFFFFFFFFFFF - 2"
|
||||
" 0x7FFFFFFFFFFFFFFF * 2"
|
||||
"-~0x7FFFFFFFFFFFFFFF"
|
||||
)
|
||||
math(EXPR result "${expr}")
|
||||
message(STATUS "${expr}: ${result}")
|
||||
|
||||
Reference in New Issue
Block a user