[patch] libstdc++/58038 libstdc++/60421 fix overflows in std::this_thread::sleep_for()

Jonathan Wakely jwakely@redhat.com
Thu Mar 26 19:59:00 GMT 2015


We have a couple of bugs where durations with unsigned representations
result in negative or huge time_t values, which result in not sleeping
at all or sleeping for billions of years (or merely for decades if you
have a 32-bit time_t).

This change simply returns early for time points in the past or
negative durations.

Tested x86_64-linux, powerpc64le-linux, committed to trunk.


For stage 1 ...

We can still overflow in this_thread::sleep_for() here:

  auto __s = chrono::duration_cast<chrono::seconds>(__rtime);

if __rtime uses a uint64_t rep and has a value larger than
chrono::seconds::max(), and also here:

      static_cast<std::time_t>(__s.count()),

if time_t is 32-bit and __s has a value larger than it can hold.

We should limit the __s value to duration<time_t>::max() but detecting
those overflows reliably is more complicated than we can manage for
gcc5, and only happens for crazy durations anyway.

We should also handle nanosleep() returning early and setting EINTR,
and also loop in sleep_until() to handle clock adjustments:

  auto __now = _Clock::now();
  if (_Clock::is_steady)
    {
      if (__now < __atime)
        sleep_for(__atime - __now);
      return;
    }
  while (__now < __atime)
    {
      sleep_for(__atime - __now);
      __now = _Clock::now();
    }

I also need to finish reviewing <condition_variable>, <mutex> and
<shared_mutex> for similar problems.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 4313 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20150326/01907435/attachment.bin>


More information about the Gcc-patches mailing list