Skip to content

time.h: fix integer overflow in MsToAbsoluteTimespec() on 32-bit architectures #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions app/src/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions app/tests/time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these value test for previous overflow scenarios?

If so a comment saying that this tests for overlfow on 32 bit systems would be helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as the test gets run on a 32-bit architecture it will detect the overflow. I'm not sure how our GitHub Actions are set up and if they trigger these tests on 32-bit platforms. I've opened a temporary PR #1043 to see if any of the GitHub Actions fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also added this inline comment to time_test.cc:

This test verifies the fix for the old integer overflow bug on 32-bit architectures: #1042.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified the fix using GitHub Actions in #1043

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
Expand Down
4 changes: 4 additions & 0 deletions release_build_files/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down