Skip to content

Preprocessor constant #defines can overflow on embedded platforms with small integer types #1185

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
dylanmckay opened this issue Dec 9, 2017 · 2 comments

Comments

@dylanmckay
Copy link
Contributor

Some architectures have small integer types such as AVR, which has 16-bit int.

For preprocessor constants, bindgen emits the constant in Rust as a constant with type libc::c_int. If c_int is i16, large constants can overflow, causing compiler warnings.

This is a pretty obscure bug because it only appears on esoteric platforms with a small int type. At some point bindgen could be modified so that it detects constant overflow and promotes to the next bigger integer type.

Input C/C++ Header

#define NTP_OFFSET 3155673600
#define UNIX_OFFSET 946684800

Actual Results

warning: literal out of range for u16
   --> src/bindings.rs:162:48
    |
162 | pub const UNIX_OFFSET: ::rust_ctypes::c_uint = 946684800;
    |                                                ^^^^^^^^^

warning: literal out of range for u16
   --> src/bindings.rs:163:47
    |
163 | pub const NTP_OFFSET: ::rust_ctypes::c_uint = 3155673600;
    |                                               ^^^^^^^^^^

warning: literal out of range for u16
   --> src/bindings.rs:864:53
    |
864 | pub const __AVR_LIBC_DATE_: ::rust_ctypes::c_uint = 20150209;

Expected Results

Bindgen maps the preprocessor defines to Rust constants which have types large enough to fit the integer literal.

@emilio
Copy link
Contributor

emilio commented Jan 31, 2018

Huh, interesting, I'd have bet that we mapped them to actual fixed-sized types.

You can workaround it with ParseCallbacks. This can also be trivially fixed in var.rs, where the type is chosen. It's late for me now, but I'm happy to mentor or take a look tomorrow.

emilio added a commit to emilio/rust-bindgen that referenced this issue Feb 4, 2018
emilio added a commit to emilio/rust-bindgen that referenced this issue Feb 4, 2018
bors-servo pushed a commit that referenced this issue Feb 4, 2018
ir: Make macro constants not being architecture-dependent.

Fixes #1185
@emilio
Copy link
Contributor

emilio commented Feb 4, 2018

#1246

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

No branches or pull requests

3 participants