From 758088c673175b70f198b64c02c21d122cbecb4f Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 17 Jul 2016 01:24:20 +0000 Subject: [PATCH] Fix Stack::new(0). --- src/os/mod.rs | 5 ++++- tests/stack.rs | 9 ++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/os/mod.rs b/src/os/mod.rs index e977c18..67c7af0 100644 --- a/src/os/mod.rs +++ b/src/os/mod.rs @@ -28,9 +28,12 @@ impl Stack { pub fn new(size: usize) -> Result { 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 // 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. let len = len + page_size; diff --git a/tests/stack.rs b/tests/stack.rs index 9315784..6c9f049 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -6,7 +6,14 @@ extern crate fringe; use fringe::{Stack, OsStack}; #[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(); // Make sure the topmost page of the stack, at least, is accessible. unsafe { *(stack.top().offset(-1)) = 0; }