Skip to content

Commit cbd82eb

Browse files
FlamefireFlow86
authored andcommitted
Fix UB in test
The maximum value for a random point was INT_MAX/2. We had at some point: `point + size*2` which overflows when size is in the range of INT_MAX/2. Use an upper bound of /32 for the random point values.
1 parent 5b88cd6 commit cbd82eb

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

tests/s25Main/UI/testControls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ BOOST_AUTO_TEST_SUITE(Controls)
5858

5959
BOOST_FIXTURE_TEST_CASE(MouseOver, uiHelper::Fixture)
6060
{
61-
const auto pos = rttr::test::randomPoint<DrawPoint>(0, std::numeric_limits<DrawPoint::ElementType>::max() / 2);
62-
const auto size = rttr::test::randomPoint<Extent>(10, std::numeric_limits<DrawPoint::ElementType>::max() / 2);
61+
const auto pos = rttr::test::randomPoint<DrawPoint>();
62+
const auto size = rttr::test::randomPoint<Extent>(10);
6363
const auto font = createMockFont({'H', 'e', 'l', 'o', '?'});
6464
ctrlTextButton bt(nullptr, 1, pos, size, TextureColor::Bricks, "Hello", font.get(), "");
6565
BOOST_TEST(bt.IsMouseOver(pos));

tests/testHelpers/rttr/test/random.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ inline bool randomBool()
2929
}
3030
template<typename T>
3131
auto randomPoint(typename T::ElementType min = std::numeric_limits<typename T::ElementType>::min(),
32-
typename T::ElementType max = std::numeric_limits<typename T::ElementType>::max())
32+
// Avoid overflow in some calculations by limiting max
33+
typename T::ElementType max = std::numeric_limits<typename T::ElementType>::max() / 32)
3334
{
3435
return T{randomValue(min, max), randomValue(min, max)};
3536
}

0 commit comments

Comments
 (0)