Addition and subtraction overflow

740 views
Skip to first unread message

Maurice Shih

unread,
Nov 13, 2017, 12:55:28 AM11/13/17
to Tock Embedded OS Development Discussion

I'm testing code in the kernel and if I rust say this,

let mut cbreak:u32 = <u32>::max_value();
cbreak += 1;
println!("Done... {}", cbreak);

and run it  it shows "Done... 0", which is correct. However, if I try to do it in a standard rust installation on my pc for example (outside of Tock), it says

thread 'main' panicked at 'attempt to add with overflow', rec.rs:117:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.

Should I know allow addition and subtraction overflow as seen in the first example? If so, is there a way to disable the panic outside of Tock?


niklasad...@gmail.com

unread,
Nov 13, 2017, 2:19:09 AM11/13/17
to Tock Embedded OS Development Discussion
Hi Maurice,
I think the discrepancy is that in Tock you run in Rust release-mode and on your PC you run Rust in debug-mode.
Debug-mode in Rust checks for integer overflow and panics, disable it by "$ cargo run --release"
If you want to to detect integer overflow in Tock use "checked Arithmetic", see  https://doc.rust-lang.org/std/primitive.usize.html#method.checked_add

--
Yours
Niklas A
Reply all
Reply to author
Forward
0 new messages