libstdc++: Avoid more 32-bit time_t overflows in futex calls
authorJonathan Wakely <jwakely@redhat.com>
Fri, 13 Nov 2020 19:11:02 +0000 (19:11 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 13 Nov 2020 19:11:07 +0000 (19:11 +0000)
commit91004436daaf8d54daa467908d1b634a1a352707
tree2f449da8e4fab14f7f038e609453b7392393e863
parent500e7efee91cc66f91f993f53039983f131f7075
libstdc++: Avoid more 32-bit time_t overflows in futex calls

This fixes another overflow in code converting a std::chrono::seconds
duration to a time_t. This time in the new code using a futex wait with
an absolute timeout (so this one doesn't need to be backported to the
release branches).

A timeout after the epochalypse would overflow the tv_sec field,
producing an incorrect value. If that incorrect value happened to be
negative, the syscall would return with EINVAL and then the caller would
keep retrying, spinning until the timeout was reached.  If the value
happened to be positive, we would wake up too soon and incorrectly
report a timeout

libstdc++-v3/ChangeLog:

* src/c++11/futex.cc (relative_timespec): Add [[unlikely]]
attributes.
(__atomic_futex_unsigned_base::_M_futex_wait_until)
(__atomic_futex_unsigned_base::_M_futex_wait_until_steady):
Check for overflow.
* testsuite/30_threads/future/members/wait_until_overflow.cc:
New test.
libstdc++-v3/src/c++11/futex.cc
libstdc++-v3/testsuite/30_threads/future/members/wait_until_overflow.cc [new file with mode: 0644]