Skip to content

Commit ed0d970

Browse files
authored
time.h: fix integer overflow in MsToAbsoluteTimespec() on 32-bit architectures (#1042)
1 parent 05890ac commit ed0d970

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

app/src/time.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,13 @@ inline timespec MsToTimespec(int milliseconds) {
8989
inline timespec MsToAbsoluteTimespec(int milliseconds) {
9090
timespec t;
9191
clock_gettime(CLOCK_REALTIME, &t);
92-
t.tv_nsec += milliseconds * internal::kNanosecondsPerMillisecond;
93-
NormalizeTimespec(&t);
92+
93+
const int64_t nanoseconds =
94+
t.tv_nsec + (t.tv_sec * internal::kNanosecondsPerSecond) +
95+
(milliseconds * internal::kNanosecondsPerMillisecond);
96+
97+
t.tv_sec = nanoseconds / internal::kNanosecondsPerSecond;
98+
t.tv_nsec = nanoseconds % internal::kNanosecondsPerSecond;
9499
return t;
95100
}
96101

app/tests/time_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ TEST(TimeTests, ComparisonTests) {
6262
EXPECT_EQ(firebase::internal::TimespecCmp(t1, t1), 0);
6363
EXPECT_EQ(firebase::internal::TimespecCmp(t2, t2), 0);
6464
}
65+
66+
// This test verifies the fix for the old integer overflow bug on 32-bit
67+
// architectures: https://github.com/firebase/firebase-cpp-sdk/pull/1042.
68+
TEST(TimeTests, MsToAbsoluteTimespecTest) {
69+
const timespec t1 = firebase::internal::MsToAbsoluteTimespec(0);
70+
const timespec t2 = firebase::internal::MsToAbsoluteTimespec(10000);
71+
const int64_t ms1 = firebase::internal::TimespecToMs(t1);
72+
const int64_t ms2 = firebase::internal::TimespecToMs(t2);
73+
ASSERT_NEAR(ms1, ms2 - 10000, 300);
74+
}
6575
#endif
6676

6777
// Test GetTimestamp function

release_build_files/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,10 @@ code.
640640
cause duplicate symbol linker errors in conjunction with other libraries
641641
([#989](https://github.com/firebase/firebase-cpp-sdk/issues/989)).
642642
- GMA (iOS): Updated iOS dependency to Google Mobile Ads SDK version 9.7.0.
643+
- General (Android,iOS,Linux,macOS 32-bit): Fixed an integer overflow which
644+
could result in a crash or premature return when waiting for a `Future`
645+
with a timeout
646+
([#1042](https://github.com/firebase/firebase-cpp-sdk/pull/1042)).
643647

644648
### 9.3.0
645649
- Changes

0 commit comments

Comments
 (0)