Skip to content

Commit 8079103

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 ef934e0 commit 8079103

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
@@ -300,7 +300,9 @@ platform_time(uint64_t nsec) {
300300
static inline dispatch_time_t
301301
clock_and_value_to_time(int clock, long long sec, long long nsec) {
302302
uint64_t deadline;
303-
if (__builtin_mul_overflow(sec, NSEC_PER_SEC, &deadline)
303+
if (sec < 0 || sec == 0 && nsec < 0)
304+
deadline = 0;
305+
else if (__builtin_mul_overflow(sec, NSEC_PER_SEC, &deadline)
304306
|| __builtin_add_overflow(nsec, deadline, &deadline)) {
305307
deadline = UINT64_MAX;
306308
}
@@ -352,8 +354,10 @@ void swift_dispatchEnqueueWithDeadline(bool global,
352354

353355
if (tnsec != -1) {
354356
uint64_t leeway;
355-
if (__builtin_mul_overflow(tsec, NSEC_PER_SEC, &leeway)
356-
|| __builtin_add_overflow(tnsec, leeway, &leeway)) {
357+
if (tsec < 0 || tsec == 0 && tnsec < 0)
358+
leeway = 0;
359+
else if (__builtin_mul_overflow(tsec, NSEC_PER_SEC, &leeway)
360+
|| __builtin_add_overflow(tnsec, leeway, &leeway)) {
357361
leeway = UINT64_MAX;
358362
}
359363

0 commit comments

Comments
 (0)