Skip to content

Commit

Permalink
Merge pull request #21113 from kungf/scrub_interval_overflow
Browse files Browse the repository at this point in the history
utime: fix __32u sec time overflow

Reviewed-by: Kefu Chai <kchai@redhat.com>
  • Loading branch information
liewegas committed Apr 17, 2018
2 parents 3d1e7ee + dd2ee8d commit 6cfd3f7
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/include/utime.h
Expand Up @@ -32,6 +32,9 @@
// --------
// utime_t

inline __u32 cap_to_u32_max(__u64 t) {
return std::min(t, (__u64)std::numeric_limits<uint32_t>::max());
}
/* WARNING: If add member in utime_t, please make sure the encode/decode funtion
* work well. For little-endian machine, we should make sure there is no padding
* in 32-bit machine and 64-bit machine.
Expand All @@ -47,9 +50,10 @@ class utime_t {
bool is_zero() const {
return (tv.tv_sec == 0) && (tv.tv_nsec == 0);
}

void normalize() {
if (tv.tv_nsec > 1000000000ul) {
tv.tv_sec += tv.tv_nsec / (1000000000ul);
tv.tv_sec = cap_to_u32_max(tv.tv_sec + tv.tv_nsec / (1000000000ul));
tv.tv_nsec %= 1000000000ul;
}
}
Expand Down Expand Up @@ -433,22 +437,21 @@ class utime_t {
WRITE_CLASS_ENCODER(utime_t)
WRITE_CLASS_DENC(utime_t)


// arithmetic operators
inline utime_t operator+(const utime_t& l, const utime_t& r) {
return utime_t( l.sec() + r.sec() + (l.nsec()+r.nsec())/1000000000L,
(l.nsec()+r.nsec())%1000000000L );
__u64 sec = (__u64)l.sec() + r.sec();
return utime_t(cap_to_u32_max(sec), l.nsec() + r.nsec());
}
inline utime_t& operator+=(utime_t& l, const utime_t& r) {
l.sec_ref() += r.sec() + (l.nsec()+r.nsec())/1000000000L;
l.sec_ref() = cap_to_u32_max((__u64)l.sec() + r.sec());
l.nsec_ref() += r.nsec();
l.nsec_ref() %= 1000000000L;
l.normalize();
return l;
}
inline utime_t& operator+=(utime_t& l, double f) {
double fs = trunc(f);
double ns = (f - fs) * 1000000000.0;
l.sec_ref() += (long)fs;
l.sec_ref() = cap_to_u32_max(l.sec() + (__u64)fs);
l.nsec_ref() += (long)ns;
l.normalize();
return l;
Expand Down

0 comments on commit 6cfd3f7

Please sign in to comment.