math: Avoid FPE on INT_MIN by -1

This commit is contained in:
Tyler Yankee
2026-04-27 09:22:26 -04:00
parent e35a445d6d
commit 57444e1d3f
8 changed files with 22 additions and 0 deletions
+6
View File
@@ -190,6 +190,9 @@ std::int64_t cmExprParserHelper::Div(std::int64_t l, std::int64_t r)
if (r == 0) {
throw std::overflow_error("divide by zero");
}
if (l == INT64_MIN && r == -1) {
throw std::overflow_error("signed integer overflow in division");
}
return l / r;
}
@@ -198,6 +201,9 @@ std::int64_t cmExprParserHelper::Mod(std::int64_t l, std::int64_t r)
if (r == 0) {
throw std::overflow_error("modulo by zero");
}
if (l == INT64_MIN && r == -1) {
throw std::overflow_error("signed integer overflow in modulo");
}
return l % r;
}
@@ -0,0 +1 @@
1
@@ -0,0 +1,5 @@
^CMake Error at MATH-DivideMinByMinusOne\.cmake:1 \(math\):
math cannot evaluate the expression: "~9223372036854775807/-1": signed
integer overflow in division\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)$
@@ -0,0 +1 @@
math(EXPR var "~9223372036854775807/-1")
@@ -0,0 +1 @@
1
@@ -0,0 +1,5 @@
^CMake Error at MATH-ModMinByMinusOne\.cmake:1 \(math\):
math cannot evaluate the expression: "~9223372036854775807%-1": signed
integer overflow in modulo\.
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)$
@@ -0,0 +1 @@
math(EXPR var "~9223372036854775807%-1")
+2
View File
@@ -8,6 +8,8 @@ run_cmake(MATH-InvalidExpression)
run_cmake(MATH-ToleratedExpression)
run_cmake(MATH-DivideByZero)
run_cmake(MATH-ModByZero)
run_cmake(MATH-DivideMinByMinusOne)
run_cmake(MATH-ModMinByMinusOne)
if(CMake_TEST_MATH_OVERFLOW)
run_cmake_script(Overflow)