Skip to content

const T some_array[]; is compiled as static mut some_array: [T; 0usize];, which seems wrong. #2290

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
thomcc opened this issue Sep 29, 2022 · 1 comment · Fixed by #2301
Closed

Comments

@thomcc
Copy link
Member

thomcc commented Sep 29, 2022

For some reason, a C header containing a declaration like:

extern const char some_static_string[];

is compiled to a rust bindings file with the following declaration:

extern "C" {
    pub static mut some_static_string: [::std::os::raw::c_char; 0usize];
}

This is wrong for a few reasons:

  1. It's static mut, even though the input is a constant (in readonly memory, even).
  2. It may be UB to read past the end (possibly even if done carefully with something core::ptr::addr_of! or similar): See Are statics confined to the size indicated by their type? unsafe-code-guidelines#259.

In theory the second might be a bigger problem (since maybe it would cause the symbol not to point at the right thing), but in practice it doesn't seem like anything goes wrong. And I have no suggestion for an alternative.

The first is less subtle, and has a more obvious fix (ditch the mut), so it's probably worth fixing. That said, it's probably harmless (so long as you never write to it... which requires unsafe anyway).

@pvdrz
Copy link
Contributor

pvdrz commented Oct 7, 2022

So this basically happens because clang is not able to evaluate some_static_string due to being external and at the same time clang_isConstQualifiedType returns false (not sure why though). This seems like a similar instance of #1727 so maybe we should special case incomplete arrays too.

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