about summary refs log tree commit diff
path: root/src/libstd/rt/mod.rs
diff options
context:
space:
mode:
authorFelix S. Klock II <pnkfelix@pnkfx.org>2015-03-03 12:07:48 +0100
committerFelix S. Klock II <pnkfelix@pnkfx.org>2015-03-03 13:12:38 +0100
commit243c5164ea32b38c4ac44fdd5e0ceb2da45c283f (patch)
tree21527ebe06a3c31ba400395c0b8dc633f00e6cbf /src/libstd/rt/mod.rs
parent185c074798ce87429118868c292d2c2c7dc46cfc (diff)
downloadrust-243c5164ea32b38c4ac44fdd5e0ceb2da45c283f.tar.gz
rust-243c5164ea32b38c4ac44fdd5e0ceb2da45c283f.zip
sidestep potential over- and underflow in estimated stack bounds.
See buildlog here for evidence of such occurring:
  http://buildbot.rust-lang.org/builders/auto-linux-32-opt/builds/3910/steps/test/logs/stdio
Diffstat (limited to 'src/libstd/rt/mod.rs')
-rw-r--r--src/libstd/rt/mod.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs
index 42cca73e5e2..fe32a51e81c 100644
--- a/src/libstd/rt/mod.rs
+++ b/src/libstd/rt/mod.rs
@@ -27,6 +27,7 @@ use marker::Send;
 use ops::FnOnce;
 use sys;
 use thunk::Thunk;
+use usize;
 
 // Reexport some of our utilities which are expected by other crates.
 pub use self::util::{default_sched_threads, min_stack, running_on_valgrind};
@@ -78,7 +79,20 @@ fn lang_start(main: *const u8, argc: int, argv: *const *const u8) -> int {
     // FIXME #11359 we just assume that this thread has a stack of a
     // certain size, and estimate that there's at most 20KB of stack
     // frames above our current position.
-    let my_stack_bottom = my_stack_top + 20000 - OS_DEFAULT_STACK_ESTIMATE;
+    const TWENTY_KB: uint = 20000;
+
+    // saturating-add to sidestep overflow
+    let top_plus_spill = if usize::MAX - TWENTY_KB < my_stack_top {
+        usize::MAX
+    } else {
+        my_stack_top + TWENTY_KB
+    };
+    // saturating-sub to sidestep underflow
+    let my_stack_bottom = if top_plus_spill < OS_DEFAULT_STACK_ESTIMATE {
+        0
+    } else {
+        top_plus_spill - OS_DEFAULT_STACK_ESTIMATE
+    };
 
     let failed = unsafe {
         // First, make sure we don't trigger any __morestack overflow checks,