about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2016-06-19 15:58:40 +0200
committerggomez <guillaume1.gomez@gmail.com>2016-06-21 15:50:27 +0200
commitc02414e9bd4326d3db8d54a1cf4707089737b414 (patch)
tree2d8b3b7e914b10c3721663762212c0ad30ff815c /src/libstd/sys
parent3313e50594aeb8e81dbe7bac27addcf41be40f9c (diff)
downloadrust-c02414e9bd4326d3db8d54a1cf4707089737b414.tar.gz
rust-c02414e9bd4326d3db8d54a1cf4707089737b414.zip
Fix overflow error in thread::sleep
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/unix/thread.rs21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/libstd/sys/unix/thread.rs b/src/libstd/sys/unix/thread.rs
index cb34d1a5fbc..371319a93d2 100644
--- a/src/libstd/sys/unix/thread.rs
+++ b/src/libstd/sys/unix/thread.rs
@@ -125,16 +125,25 @@ impl Thread {
     }
 
     pub fn sleep(dur: Duration) {
-        let mut ts = libc::timespec {
-            tv_sec: dur.as_secs() as libc::time_t,
-            tv_nsec: dur.subsec_nanos() as libc::c_long,
-        };
+        let mut secs = dur.as_secs();
+        let mut nsecs = dur.subsec_nanos() as libc::c_long;
 
         // If we're awoken with a signal then the return value will be -1 and
         // nanosleep will fill in `ts` with the remaining time.
         unsafe {
-            while libc::nanosleep(&ts, &mut ts) == -1 {
-                assert_eq!(os::errno(), libc::EINTR);
+            while secs > 0 || nsecs > 0 {
+                let mut ts = libc::timespec {
+                    tv_sec: cmp::min(libc::time_t::max_value() as u64, secs) as libc::time_t,
+                    tv_nsec: nsecs,
+                };
+                secs -= ts.tv_sec as u64;
+                if libc::nanosleep(&ts, &mut ts) == -1 {
+                    assert_eq!(os::errno(), libc::EINTR);
+                    secs += ts.tv_sec as u64;
+                    nsecs = ts.tv_nsec;
+                } else {
+                    nsecs = 0;
+                }
             }
         }
     }