scarfS
Denoβ€’3y agoβ€’
3 replies
scarf

rust ffi buffer overflow

hi, do i have to null-terminate array when using rust ffi? i'm getting buffer overflows.
#[no_mangle]
pub extern "C" fn play(path: *const u8) {
    let path = unsafe {
        assert!(!path.is_null());

        std::ffi::CStr::from_ptr(path as *const i8)
    };
    let path = path.to_str().unwrap();
    println!("path: {path}");
}

const lib = "./audio/target/debug/libaudio.so"
const dylib = Deno.dlopen(lib, { "play": { parameters: ["buffer"], result: "void" } } as const)

const encoded = new TextEncoder().encode("path")

dylib.symbols.play(encoded)

$ deno run -A --unstable --allow-ffi audio.ts
thread '<unnamed>' panicked at src/lib.rs:29:30:
called `Result::unwrap()` on an `Err` value: Utf8Error { valid_up_to: 3, error_len: Some(1) }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
Was this page helpful?