Skip to content

Commit b626f65

Browse files
ZXShadyChrisThrasher
authored andcommitted
Cast to larger type before doing arithmetic to avoid signed integer overflow
https://godbolt.org/z/soTad4zEq this godbolt links shows that it is UB to multiply then cast if the amount is large enough
1 parent f297287 commit b626f65

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/CSFML/System/Time.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ sfTime sfSeconds(float amount)
6363
////////////////////////////////////////////////////////////
6464
sfTime sfMilliseconds(int32_t amount)
6565
{
66-
return {static_cast<int64_t>(amount * 1000)};
66+
return {int64_t{amount} * 1000};
6767
}
6868

6969

test/System/Time.test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ TEST_CASE("[System] sfTime")
2828
CHECK(sfSeconds(10).microseconds == 10'000'000);
2929
CHECK(sfMilliseconds(10).microseconds == 10'000);
3030
CHECK(sfMicroseconds(10).microseconds == 10);
31+
32+
CHECK(sfMilliseconds(std::numeric_limits<int32_t>::max()).microseconds == 2'147'483'647'000);
33+
CHECK(sfMicroseconds(std::numeric_limits<int64_t>::max()).microseconds == std::numeric_limits<int64_t>::max());
3134
}

0 commit comments

Comments
 (0)