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:
Brad King
2026-03-04 09:41:56 -05:00
parent d140e01447
commit 2423db545f
4 changed files with 15 additions and 1 deletions
+4
View File
@@ -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;
}
+8
View File
@@ -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\.$
+2 -1
View File
@@ -6,4 +6,5 @@
-- 4 >> 65: 2
-- 0x7FFFFFFFFFFFFFFF \+ 1: -9223372036854775808
-- -0x7FFFFFFFFFFFFFFF - 2: 9223372036854775807
-- 0x7FFFFFFFFFFFFFFF \* 2: -2$
-- 0x7FFFFFFFFFFFFFFF \* 2: -2
-- -~0x7FFFFFFFFFFFFFFF: -9223372036854775808$
+1
View File
@@ -8,6 +8,7 @@ foreach(expr IN ITEMS
" 0x7FFFFFFFFFFFFFFF + 1"
"-0x7FFFFFFFFFFFFFFF - 2"
" 0x7FFFFFFFFFFFFFFF * 2"
"-~0x7FFFFFFFFFFFFFFF"
)
math(EXPR result "${expr}")
message(STATUS "${expr}: ${result}")