libc, librthread: _twait: subtraction is not comparison
authorcheloha <cheloha@openbsd.org>
Sun, 7 Jan 2024 19:44:28 +0000 (19:44 +0000)
committercheloha <cheloha@openbsd.org>
Sun, 7 Jan 2024 19:44:28 +0000 (19:44 +0000)
commit2e686a7fd2561c81dde123d8a2bdf2f23b1bce4b
treed77898dbb74c702a230af5390831378305166eca
parentcec65c07f7ecd478038217fa6c0ddf0d2922749a
libc, librthread: _twait: subtraction is not comparison

Compare the current time with the absolute timeout before computing
the relative timeout to avoid arithmetic overflow.  Fixes a bug where
large negative absolute timeouts are subtracted into large positive
relative timeouts and incorrectly cause the caller to block.

While here, use timespeccmp(3) and timespecsub(3) to simplify the
code.

Thread: https://marc.info/?l=openbsd-tech&m=169945962503129&w=2
lib/libc/thread/synch.h
lib/librthread/synch.h