Skip to content

Commit 243c516

Browse files
committed
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
1 parent 185c074 commit 243c516

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Diff for: src/libstd/rt/mod.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use marker::Send;
2727
use ops::FnOnce;
2828
use sys;
2929
use thunk::Thunk;
30+
use usize;
3031

3132
// Reexport some of our utilities which are expected by other crates.
3233
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 {
7879
// FIXME #11359 we just assume that this thread has a stack of a
7980
// certain size, and estimate that there's at most 20KB of stack
8081
// frames above our current position.
81-
let my_stack_bottom = my_stack_top + 20000 - OS_DEFAULT_STACK_ESTIMATE;
82+
const TWENTY_KB: uint = 20000;
83+
84+
// saturating-add to sidestep overflow
85+
let top_plus_spill = if usize::MAX - TWENTY_KB < my_stack_top {
86+
usize::MAX
87+
} else {
88+
my_stack_top + TWENTY_KB
89+
};
90+
// saturating-sub to sidestep underflow
91+
let my_stack_bottom = if top_plus_spill < OS_DEFAULT_STACK_ESTIMATE {
92+
0
93+
} else {
94+
top_plus_spill - OS_DEFAULT_STACK_ESTIMATE
95+
};
8296

8397
let failed = unsafe {
8498
// First, make sure we don't trigger any __morestack overflow checks,

0 commit comments

Comments
 (0)