rust-lang / rust-lang/rust-bindgen
Padding not added if we use a typedef to a wrongly-sized replacement type
Open
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Reproduction:
/**
* <div rustbindgen="true" replaces="std::string">
*/
class CxxString {
char* ptr;
};
#include <string>
#include <cstdint>
typedef std::string my_string;
struct A {
my_string a;
};
struct B {
std::string a;
};
Command:
cargo run -- test.hpp --no-layout-tests --allowlist-type A --allowlist-type B --enable-cxx-namespaces
Bindgen version 20aa65a0b9edfd5f8ab3e038197da5cb2c52ff18 (today's main).
Rust generated:
/* automatically generated by rust-bindgen 0.71.1 */
#[allow(non_snake_case, non_camel_case_types, non_upper_case_globals)]
pub mod root {
#[allow(unused_imports)]
use self::super::root;
pub mod std {
#[allow(unused_imports)]
use self::super::super::root;
#[doc = " <div rustbindgen=\"true\" replaces=\"std::string\">"]
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct string {
pub ptr: *mut ::std::os::raw::c_char,
}
}
pub type my_string = root::std::string;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct A {
pub a: root::my_string,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct B {
pub a: root::std::string,
pub __bindgen_padding_0: [u64; 2usize],
}
}
Note that struct B contains padding, but struct A doesn't. This means the size of struct A is wrong.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the test.hpp example and the shown cargo run command, comparing the generated layouts for A and B. Start by tracing bindgen's handling of typedef aliases and replacement types in layout generation; done means struct A receives the padding needed to match its C++ size.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100