Skip to content

Commit

Permalink
Fix signed integer overflow in test_util_math
Browse files Browse the repository at this point in the history
  • Loading branch information
msimberg committed Feb 21, 2024
1 parent 1667a69 commit d2d997c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions test/unit/test_util_math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ TYPED_TEST(MathUtilTest, ptrdiff_t_Arithmetic_Sum) {
constexpr auto arithmetic_size = sizeof(SizeType);

TypeParam a, b;
ArithmeticT c = std::numeric_limits<ArithmeticT>::max() / 2;

if (type_size < arithmetic_size) {
a = std::numeric_limits<TypeParam>::max();
Expand All @@ -155,8 +156,8 @@ TYPED_TEST(MathUtilTest, ptrdiff_t_Arithmetic_Sum) {
}

{
auto expected_result = static_cast<ArithmeticT>(a) + std::numeric_limits<ArithmeticT>::max();
EXPECT_EQ(expected_result, util::ptrdiff_t::sum(a, std::numeric_limits<ArithmeticT>::max()));
auto expected_result = static_cast<ArithmeticT>(a) + c;
EXPECT_EQ(expected_result, util::ptrdiff_t::sum(a, c));
}
}

Expand All @@ -168,7 +169,7 @@ TYPED_TEST(MathUtilTest, ptrdiff_t_Arithmetic_Mul) {

TypeParam a, b;

if (type_size < arithmetic_size) {
if (type_size * 2 < arithmetic_size) {
a = std::numeric_limits<TypeParam>::max();
b = std::numeric_limits<TypeParam>::max();
}
Expand Down

0 comments on commit d2d997c

Please sign in to comment.