diff --git a/app/src/time.h b/app/src/time.h index c52bd2ad67..a60bd7a3e5 100644 --- a/app/src/time.h +++ b/app/src/time.h @@ -89,8 +89,13 @@ inline timespec MsToTimespec(int milliseconds) { inline timespec MsToAbsoluteTimespec(int milliseconds) { timespec t; clock_gettime(CLOCK_REALTIME, &t); - t.tv_nsec += milliseconds * internal::kNanosecondsPerMillisecond; - NormalizeTimespec(&t); + + const int64_t nanoseconds = + t.tv_nsec + (t.tv_sec * internal::kNanosecondsPerSecond) + + (milliseconds * internal::kNanosecondsPerMillisecond); + + t.tv_sec = nanoseconds / internal::kNanosecondsPerSecond; + t.tv_nsec = nanoseconds % internal::kNanosecondsPerSecond; return t; } diff --git a/app/tests/time_test.cc b/app/tests/time_test.cc index 09daf7a2a4..a2a2c0ea77 100644 --- a/app/tests/time_test.cc +++ b/app/tests/time_test.cc @@ -62,6 +62,16 @@ TEST(TimeTests, ComparisonTests) { EXPECT_EQ(firebase::internal::TimespecCmp(t1, t1), 0); EXPECT_EQ(firebase::internal::TimespecCmp(t2, t2), 0); } + +// This test verifies the fix for the old integer overflow bug on 32-bit +// architectures: https://github.com/firebase/firebase-cpp-sdk/pull/1042. +TEST(TimeTests, MsToAbsoluteTimespecTest) { + const timespec t1 = firebase::internal::MsToAbsoluteTimespec(0); + const timespec t2 = firebase::internal::MsToAbsoluteTimespec(10000); + const int64_t ms1 = firebase::internal::TimespecToMs(t1); + const int64_t ms2 = firebase::internal::TimespecToMs(t2); + ASSERT_NEAR(ms1, ms2 - 10000, 300); +} #endif // Test GetTimestamp function diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 184f42d005..a1045ae643 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -640,6 +640,10 @@ code. cause duplicate symbol linker errors in conjunction with other libraries ([#989](https://github.com/firebase/firebase-cpp-sdk/issues/989)). - GMA (iOS): Updated iOS dependency to Google Mobile Ads SDK version 9.7.0. + - General (Android,iOS,Linux,macOS 32-bit): Fixed an integer overflow which + could result in a crash or premature return when waiting for a `Future` + with a timeout + ([#1042](https://github.com/firebase/firebase-cpp-sdk/pull/1042)). ### 9.3.0 - Changes