diff options
author | 2020-08-26 21:41:13 -0700 | |
---|---|---|
committer | 2020-08-28 15:47:40 +0000 | |
commit | ca8343842f9094fd5eb86569d293250e783f582c (patch) | |
tree | c4e040eff9f8528626535106a88fe4e7bb317a00 /libartbase/base/time_utils.h | |
parent | 49a19f38c35605e675eee271691ed465802859bf (diff) |
Avoid NanoSleep overflow
NanoSleep with a very large argument could cause it to fail on 32 bits.
It doesn't appear to me that this was ever exposed to client code.
So this was probably not an observable bug.
Remove redundant uses of "constexpr inline" instead of adding another
one.
Bug: 161006928
Test: Treehugger
Change-Id: I2ad3b92d01c764915ab2aac17cc72ac5c6907ed4
Diffstat (limited to 'libartbase/base/time_utils.h')
-rw-r--r-- | libartbase/base/time_utils.h | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/libartbase/base/time_utils.h b/libartbase/base/time_utils.h index 69c867cfaf..cb0ab13ef9 100644 --- a/libartbase/base/time_utils.h +++ b/libartbase/base/time_utils.h @@ -69,24 +69,33 @@ uint64_t ThreadCpuNanoTime(); uint64_t ProcessCpuNanoTime(); // Converts the given number of nanoseconds to milliseconds. -static constexpr inline uint64_t NsToMs(uint64_t ns) { +static constexpr uint64_t NsToMs(uint64_t ns) { return ns / 1000 / 1000; } // Converts the given number of milliseconds to nanoseconds -static constexpr inline uint64_t MsToNs(uint64_t ms) { +static constexpr uint64_t MsToNs(uint64_t ms) { return ms * 1000 * 1000; } // Converts the given number of milliseconds to microseconds -static constexpr inline uint64_t MsToUs(uint64_t ms) { +static constexpr uint64_t MsToUs(uint64_t ms) { return ms * 1000; } -static constexpr inline uint64_t UsToNs(uint64_t us) { +static constexpr uint64_t UsToNs(uint64_t us) { return us * 1000; } +static constexpr time_t SaturatedTimeT(int64_t secs) { + if (sizeof(time_t) < sizeof(int64_t)) { + return static_cast<time_t>(std::min(secs, + static_cast<int64_t>(std::numeric_limits<time_t>::max()))); + } else { + return secs; + } +} + #if defined(__APPLE__) #ifndef CLOCK_REALTIME // No clocks to specify on OS/X < 10.12, fake value to pass to routines that require a clock. |