Project for porting C to Rust gains Mozilla's backing (InfoWorld)
Project for porting C to Rust gains Mozilla's backing (InfoWorld)
Posted Nov 2, 2016 12:27 UTC (Wed) by excors (subscriber, #95769)In reply to: Project for porting C to Rust gains Mozilla's backing (InfoWorld) by vasvir
Parent article: Project for porting C to Rust gains Mozilla's backing (InfoWorld)
In debug mode, by default, signed and unsigned integer overflows panic (i.e. unwind the current thread's stack then terminate the thread). (Incidentally, unwinding the stack across FFI boundaries is apparently undefined behaviour, so if you ever call from C into Rust then the Rust code must either spawn a new thread to do all its work or else must be absolutely convinced it's not going to panic (which seems hard to guarantee if it's doing any arithmetic).)
In release mode, by default, overflows wrap as two's complement.
There could be compiler options to turn the checking on/off in different parts of the program.
It seems to be designed as a debugging feature, not a security feature. It's discussed as being equivalent to debug assertions, which are helpful during development but typically disabled in production for performance, so programs shouldn't rely on them to prevent exploits.
Integer types have some low-level explicit arithmetic methods: "wrapping_add" (always wraps), "saturating_add", "checked_add" (returns Some(n) or None on overflow). And for people who prefer operator overloading there's the "Wrapping" type, e.g. if "let x = Wrapping(0u32) - Wrapping(1u32)" then "x.0 == std::u32::MAX".
As far as I can tell from Corrode's source (admittedly I'm not very familiar with Literate Haskell), it uses the wrapping_add etc methods for unsigned arithmetic, and standard arithmetic operators for signed.
