From ca8343842f9094fd5eb86569d293250e783f582c Mon Sep 17 00:00:00 2001 From: Hans Boehm Date: Wed, 26 Aug 2020 21:41:13 -0700 Subject: 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 --- libartbase/base/time_utils.h | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'libartbase/base/time_utils.h') 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(std::min(secs, + static_cast(std::numeric_limits::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. -- cgit v1.2.3-59-g8ed1b