From c97c17a97deae0cdc4a6380ce4e8ea8a00938887 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 4 Aug 2022 01:37:34 -0400 Subject: [PATCH 1/6] app/src/time.h: Fix MsToAbsoluteTimespec() on 32-bit architectures --- app/src/time.h | 11 +++++++++-- app/tests/time_test.cc | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/time.h b/app/src/time.h index c52bd2ad67..4c187c0d11 100644 --- a/app/src/time.h +++ b/app/src/time.h @@ -18,6 +18,8 @@ #define FIREBASE_APP_SRC_TIME_H_ #include #include +#include +#include #include "app/src/include/firebase/internal/platform.h" @@ -89,8 +91,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 - (t.tv_sec * internal::kNanosecondsPerSecond); return t; } diff --git a/app/tests/time_test.cc b/app/tests/time_test.cc index 09daf7a2a4..bde8dcbe6e 100644 --- a/app/tests/time_test.cc +++ b/app/tests/time_test.cc @@ -62,6 +62,14 @@ TEST(TimeTests, ComparisonTests) { EXPECT_EQ(firebase::internal::TimespecCmp(t1, t1), 0); EXPECT_EQ(firebase::internal::TimespecCmp(t2, t2), 0); } + +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 From b2007676cf973f21c55d3d295be1785acedf07db Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 4 Aug 2022 01:40:16 -0400 Subject: [PATCH 2/6] add changelog entry --- release_build_files/readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 184f42d005..552f7d33fc 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -640,6 +640,8 @@ 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 when waiting for a `Future` with a timeout. ### 9.3.0 - Changes From fc5073e2c2dc71871691ca2146707eb22a6bffa7 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 4 Aug 2022 01:49:18 -0400 Subject: [PATCH 3/6] app/src/time.h: remove includes that were left behind from log-based debugging --- app/src/time.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/src/time.h b/app/src/time.h index 4c187c0d11..de32b9b937 100644 --- a/app/src/time.h +++ b/app/src/time.h @@ -18,8 +18,6 @@ #define FIREBASE_APP_SRC_TIME_H_ #include #include -#include -#include #include "app/src/include/firebase/internal/platform.h" From e0a0f79d3559b33defa47102dfb7fdde24cd6c84 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 4 Aug 2022 01:51:59 -0400 Subject: [PATCH 4/6] release_build_files/readme.md: update the change log entry --- release_build_files/readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 552f7d33fc..a1045ae643 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -641,7 +641,9 @@ code. ([#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 when waiting for a `Future` with a timeout. + 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 From b104910a3651088f4da289ae63b865f70d35569a Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Thu, 4 Aug 2022 21:24:41 -0400 Subject: [PATCH 5/6] app/src/time.h: use modulo operator --- app/src/time.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/time.h b/app/src/time.h index de32b9b937..a60bd7a3e5 100644 --- a/app/src/time.h +++ b/app/src/time.h @@ -95,7 +95,7 @@ inline timespec MsToAbsoluteTimespec(int milliseconds) { (milliseconds * internal::kNanosecondsPerMillisecond); t.tv_sec = nanoseconds / internal::kNanosecondsPerSecond; - t.tv_nsec = nanoseconds - (t.tv_sec * internal::kNanosecondsPerSecond); + t.tv_nsec = nanoseconds % internal::kNanosecondsPerSecond; return t; } From b6a34b2a5fcf02c04dcdcf775854defe1acca15f Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Fri, 5 Aug 2022 12:40:52 -0400 Subject: [PATCH 6/6] app/tests/time_test.cc: add a comment about the integer overflow bug that MsToAbsoluteTimespecTest verifies --- app/tests/time_test.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/tests/time_test.cc b/app/tests/time_test.cc index bde8dcbe6e..a2a2c0ea77 100644 --- a/app/tests/time_test.cc +++ b/app/tests/time_test.cc @@ -63,6 +63,8 @@ TEST(TimeTests, ComparisonTests) { 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);