diff options
author | Ramkumar Ramachandra <ramkumar.ramachandra@codasip.com> | 2024-07-09 09:33:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-09 09:33:46 +0100 |
commit | f1eed011b4b28aecd07170f9b26cc0efb45904ee (patch) | |
tree | 2cb122fd5534793d4d9c14312ecc3a7afd2dd1f1 /llvm/unittests/Support/MathExtrasTest.cpp | |
parent | ad82d1c53f089937c05af11ff45798ceb5ca894e (diff) | |
download | llvm-f1eed011b4b28aecd07170f9b26cc0efb45904ee.zip llvm-f1eed011b4b28aecd07170f9b26cc0efb45904ee.tar.gz llvm-f1eed011b4b28aecd07170f9b26cc0efb45904ee.tar.bz2 |
MathExtras: add overflow query for signed-div (#97901)
5221634 (Do not trigger UB during AffineExpr parsing) noticed that
divideCeilSigned and divideFloorSigned would overflow when Numerator =
INT_MIN, and Denominator = -1. This observation has already been made by
DynamicAPInt, and it has code to check this. To avoid checks in multiple
callers, centralize this query in MathExtras, and change
divideCeilSigned/divideFloorSigned to assert on overflow.
Diffstat (limited to 'llvm/unittests/Support/MathExtrasTest.cpp')
-rw-r--r-- | llvm/unittests/Support/MathExtrasTest.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp index a557b61..a1cc609 100644 --- a/llvm/unittests/Support/MathExtrasTest.cpp +++ b/llvm/unittests/Support/MathExtrasTest.cpp @@ -523,6 +523,12 @@ TEST(MathExtras, DivideCeil) { std::numeric_limits<int64_t>::min() / 2 + 1); EXPECT_EQ(divideCeilSigned(std::numeric_limits<int64_t>::min(), 1), std::numeric_limits<int64_t>::min()); + + // Overflow. + EXPECT_TRUE( + divideSignedWouldOverflow(std::numeric_limits<int8_t>::min(), -1)); + EXPECT_TRUE( + divideSignedWouldOverflow(std::numeric_limits<int64_t>::min(), -1)); } TEST(MathExtras, DivideFloorSigned) { @@ -544,6 +550,8 @@ TEST(MathExtras, DivideFloorSigned) { std::numeric_limits<int64_t>::min() / 2); EXPECT_EQ(divideFloorSigned(std::numeric_limits<int64_t>::min(), 1), std::numeric_limits<int64_t>::min()); + + // Same overflow condition, divideSignedWouldOverflow, applies. } TEST(MathExtras, Mod) { |