Merge pull request #18601 from muxi/fix-gettimeofday-oob

Add guard to the tv_nsec field of gpr_now return value
pull/18618/head
Muxi Yan 6 years ago committed by GitHub
commit 422e32f70e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/core/lib/gpr/time_posix.cc

@ -108,6 +108,9 @@ static gpr_timespec now_impl(gpr_clock_type clock) {
now.clock_type = clock;
switch (clock) {
case GPR_CLOCK_REALTIME:
// gettimeofday(...) function may return with a value whose tv_usec is
// greater than 1e6 on iOS The case is resolved with the guard at end of
// this function.
gettimeofday(&now_tv, nullptr);
now.tv_sec = now_tv.tv_sec;
now.tv_nsec = now_tv.tv_usec * 1000;
@ -124,6 +127,16 @@ static gpr_timespec now_impl(gpr_clock_type clock) {
abort();
}
// Guard the tv_nsec field in valid range for all clock types
while (GPR_UNLIKELY(now.tv_nsec >= 1e9)) {
now.tv_sec++;
now.tv_nsec -= 1e9;
}
while (GPR_UNLIKELY(now.tv_nsec < 0)) {
now.tv_sec--;
now.tv_nsec += 1e9;
}
return now;
}
#endif

Loading…
Cancel
Save