Skip to content

Subtract underflow when generating explicit paddings #2695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yshui opened this issue Dec 12, 2023 · 2 comments · Fixed by #2696
Closed

Subtract underflow when generating explicit paddings #2695

yshui opened this issue Dec 12, 2023 · 2 comments · Fixed by #2696

Comments

@yshui
Copy link
Contributor

yshui commented Dec 12, 2023

Input C/C++ Header

#pragma pack(2)

struct Test {
        unsigned long x;
        char a;
        char b;
        char c;
};

Bindgen Invocation

$ bindgen input.h --explicit-padding

Actual Results

panicked at bindgen/codegen/struct_layout.rs:309:20:
attempt to subtract with overflow

Analysis

The reason this happens is because in saw_field_with_layout, padding bytes size is always calculated using the specified pack alignment, i.e. 2 bytes. however, if the field itself has a smaller alignment requirement, that should be used instead.

yshui pushed a commit to yshui/rust-bindgen that referenced this issue Dec 12, 2023
yshui added a commit to yshui/rust-bindgen that referenced this issue Dec 12, 2023
@yshui
Copy link
Contributor Author

yshui commented Dec 12, 2023

And padding is not generated for packed layout with align > 1, I will open another bug after this is fixed.

yshui added a commit to yshui/rust-bindgen that referenced this issue Jan 12, 2024
yshui added a commit to yshui/rust-bindgen that referenced this issue Jan 12, 2024
emilio pushed a commit that referenced this issue Jan 12, 2024
emilio pushed a commit that referenced this issue Jan 12, 2024
@Connor-GH
Copy link

This should probably still be open; affects me on 0.71.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants