Add sanity checks to futex_wait()

- handle the NULL timeout pointer case
- avoid negative cases.

Suggested by cheloha@ ok cheloha@, jsg@
This commit is contained in:
matthieu 2020-02-05 07:26:15 +00:00
parent 863ee81424
commit 8e89b73ce9

View File

@ -97,9 +97,16 @@ static inline int futex_wake(uint32_t *addr, int count)
static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout) static inline int futex_wait(uint32_t *addr, int32_t value, const struct timespec *timeout)
{ {
struct timespec tsrel, tsnow; struct timespec tsnow, tsrel;
clock_gettime(CLOCK_MONOTONIC, &tsnow);
timespecsub(timeout, &tsnow, &tsrel); if (timeout == NULL)
return futex(addr, FUTEX_WAIT, value, NULL, NULL);
clock_gettime(CLOCK_MONOTONIC, &tsnow);
if (timespeccmp(&tsnow, timeout, <))
timespecsub(timeout, &tsnow, &tsrel);
else
timespecclear(&tsrel);
return futex(addr, FUTEX_WAIT, value, &tsrel, NULL); return futex(addr, FUTEX_WAIT, value, &tsrel, NULL);
} }