Skip to content

Commit c9606ba

Browse files
committed
[Concurrency] Prevent negative sleeps from sleeping forever.
Requests to sleep until a negative timestamp would result in sleeping until `UINT64_MAX` nanoseconds from the start of the relevant clock, which is about 585 years. rdar://154346018
1 parent 92fd571 commit c9606ba

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

stdlib/public/Concurrency/DispatchGlobalExecutor.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ void swift_dispatchEnqueueWithDeadline(bool global,
333333
}
334334

335335
uint64_t deadline;
336-
if (__builtin_mul_overflow(sec, NSEC_PER_SEC, &deadline)
336+
if (sec < 0 || sec == 0 && nsec < 0)
337+
deadline = 0;
338+
else if (__builtin_mul_overflow(sec, NSEC_PER_SEC, &deadline)
337339
|| __builtin_add_overflow(nsec, deadline, &deadline)) {
338340
deadline = UINT64_MAX;
339341
}
@@ -342,8 +344,10 @@ void swift_dispatchEnqueueWithDeadline(bool global,
342344

343345
if (tnsec != -1) {
344346
uint64_t leeway;
345-
if (__builtin_mul_overflow(tsec, NSEC_PER_SEC, &leeway)
346-
|| __builtin_add_overflow(tnsec, leeway, &leeway)) {
347+
if (tsec < 0 || tsec == 0 && tnsec < 0)
348+
leeway = 0;
349+
else if (__builtin_mul_overflow(tsec, NSEC_PER_SEC, &leeway)
350+
|| __builtin_add_overflow(tnsec, leeway, &leeway)) {
347351
leeway = UINT64_MAX;
348352
}
349353

0 commit comments

Comments
 (0)