rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Invalid char array bindings generation

Open ynuwenhof opened this issue 1 year ago • 3 comments

Input C/C++ Header

const char BUFFER[] = "Hello" "\0" "World" "\0";

Bindgen Invocation

bindgen::Builder::default()
    .header("input.h")
    .generate()
    .unwrap()

Actual Results

pub const BUFFER: &[u8; 6] = b"Hello\0";

Expected Results

pub const BUFFER: &[u8; 13] = b"Hello\0World\0\0";

ynuwenhof avatar Oct 17 '23 11:10 ynuwenhof

Did a bit of digging and it looks like clang_EvalResult_getAsStr is the culprit, as it only returns a pointer and no size, and expects that literals contain no zeros.

I do not know if there's another way to get the full literal, but I tried my luck opening an issue on the LLVM project.

xTachyon avatar Oct 20 '23 18:10 xTachyon

Sounds good, thank you

ynuwenhof avatar Oct 20 '23 20:10 ynuwenhof

I linked it above, but then I realized this currently only applies to C++. For C, a static const gets transformed into an extern "C" pub static instead.

But that is strange, because the variable will not be exported from C. I assume the C++ const is transformed into a Rust const so that there is a chance to have the value, since it has internal linkage and some C++ headers may be written like that, especially before C++17. If that is the intention, then should the same apply to C, and thus a static const be transformed into a const too?

Taking an extra look, it seems that, for C++, all "global" variables (static or not, const or not) are mapped into Rust consts (with the bug mentioned here). And for C, all are mapped into extern "C" pub statics (with or without mut).

ojeda avatar Oct 30 '23 15:10 ojeda