Fix Stack::new(0).

This commit is contained in:
whitequark 2016-07-17 01:24:20 +00:00 committed by edef
parent aa364056fd
commit 758088c673
2 changed files with 12 additions and 2 deletions

View File

@ -28,9 +28,12 @@ impl Stack {
pub fn new(size: usize) -> Result<Stack, IoError> { pub fn new(size: usize) -> Result<Stack, IoError> {
let page_size = sys::page_size(); let page_size = sys::page_size();
// Stacks have to be at least one page long.
let len = if size == 0 { page_size } else { size };
// Round the length one page size up, using the fact that the page size // Round the length one page size up, using the fact that the page size
// is a power of two. // is a power of two.
let len = (size + page_size - 1) & !(page_size - 1); let len = (len + page_size - 1) & !(page_size - 1);
// Increase the length to fit the guard page. // Increase the length to fit the guard page.
let len = len + page_size; let len = len + page_size;

View File

@ -6,7 +6,14 @@ extern crate fringe;
use fringe::{Stack, OsStack}; use fringe::{Stack, OsStack};
#[test] #[test]
fn stack_accessible() { fn default_stack() {
let stack = OsStack::new(0).unwrap();
// Make sure the topmost page of the stack, at least, is accessible.
unsafe { *(stack.top().offset(-1)) = 0; }
}
#[test]
fn one_page_stack() {
let stack = OsStack::new(4096).unwrap(); let stack = OsStack::new(4096).unwrap();
// Make sure the topmost page of the stack, at least, is accessible. // Make sure the topmost page of the stack, at least, is accessible.
unsafe { *(stack.top().offset(-1)) = 0; } unsafe { *(stack.top().offset(-1)) = 0; }