Wrapping_add and normal add difference on ยตc hardware

โš“ Rust    ๐Ÿ“… 2025-07-11    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 4      

surdeus

Hello,

I am testing the integer overflow in Rust & C, a little information - I am using a tc375 board with hightec rust compiler, with C and Rust being compiled together.

Consider the below code built in debug mode with opt-level = 2, when I call test() function from C (main.c) file. For normal_add() func, I was expecting it to have a UB and for wrapped_add, I was expecting the wrapped around value.

pub fn normal_add(a: Datatype, b: Datatype) -> Datatype 
{
    a + b
}

pub fn wrapped_add(a: Datatype, b: Datatype) -> Datatype 
{
    a.wrapping_add(b)
}

#[no_mangle]
pub extern "C" fn test()
{
    let x: Datatype = normal_add(65535, 10);
    let y: Datatype = wrapped_add(65535, 10);

    unsafe 
    {
        core::ptr::write_volatile(&mut 0u16 as *mut _, x);
        core::ptr::write_volatile(&mut 0u16 as *mut _, y);
    }
}

But, I don't see any UB for normal_add in debug mode. Instead a wrapped around value is returned for both.

Does the overflow checks get disabled, when you are compiling for FFI for your rust function called from C? If yes, why does Rust behave in that way?

Thanks

4 posts - 3 participants

Read full topic

๐Ÿท๏ธ rust_feed