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

Padding not added if we use a typedef to a wrongly-sized replacement type

Open adetaylor opened this issue 9 months ago • 0 comments

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.

adetaylor avatar Feb 26 '25 11:02 adetaylor