为什么在 Rust 中使用可变参数链接和调用 C 函数会导致随机函数被调用?

Cos*_*Sin 1 function ffi variadic-functions rust rust-cargo

我正在玩 Rust FFI,我试图将printf具有可变参数的 C 函数与我的 Rust 可执行文件链接起来。运行可执行文件后,我目睹了一些奇怪的行为。

这是我的 Rust 代码:

use cty::{c_char, c_int};

extern "C" {
    fn printf(format: *const c_char, ...) -> c_int;
}

fn main() {
    unsafe {
        printf("One number: %d\n".as_ptr() as *const i8, 69);
        printf("Two numbers: %d %d\n".as_ptr() as *const i8, 11, 12);
        printf(
            "Three numbers: %d %d %d\n".as_ptr() as *const i8,
            30,
            31,
            32,
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

这是运行后的输出cargo run

cargo run
   Compiling c-bindings v0.1.0 (/home/costin/Rust/c-bindings)
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/c-bindings`
One number: 1
Two numbers: 1 11376
Three numbers: 0 0 0
Two numbers: 11 12
Three numbers: 0 0 56
Three numbers: 30 31 32
Run Code Online (Sandbox Code Playgroud)

我看起来第一个printf使用随机参数调用第二个和第三个,然后第二个printf使用随机参数调用第三个,因为预期输出应该是:

One number: 1
Two numbers: 11 12
Three numbers: 30 31 32
Run Code Online (Sandbox Code Playgroud)

谁能向我解释为什么会发生这种奇怪的行为?

kmd*_*eko 5

Rust 字符串并不像printf预期那样以 null 结尾。您需要手动\0在格式字符串末尾添加或使用CString

printf("One number: %d\n\0".as_ptr() as *const i8, 69);
Run Code Online (Sandbox Code Playgroud)
use std::ffi::CString;
let s = CString::new("One number: %d\n").unwrap();
printf(s.as_ptr(), 69);
Run Code Online (Sandbox Code Playgroud)