Skip to content
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

Something funny going on with extern double pointers #3898

Closed
eevee opened this issue Nov 1, 2012 · 2 comments
Closed

Something funny going on with extern double pointers #3898

eevee opened this issue Nov 1, 2012 · 2 comments
Labels
A-codegen Area: Code generation
Milestone

Comments

@eevee
Copy link

eevee commented Nov 1, 2012

0.4.

boolnames is a global array-of-char* that curses provides. (It's actually declared as extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolnames[];.)

https://gist.github.com/3995350

The C version spits out the entire array, starting with:

0x600aa0 0x7f6eb0dcd831 b bw
0x600aa8 0x7f6eb0dd5703 a am
0x600ab0 0x7f6eb0dd6199 x xsb

The Rust version spits out:

7f7a9ec58831 7378006278007762

...and then segfaults trying to evaluate **p. Cursory inspection reveals that the value given for *p is actually the string bw\0xb\0xs. Somehow the first level of indirection is disappearing.

@ben0x539
Copy link
Contributor

ben0x539 commented Nov 2, 2012

There is no two levels of indirection since it's an array and not a double pointer in curses. In your C code, the implicit array-to-pointer conversion does what you expect, but straight-up telling rustc that the symbol boolnames refers to a double pointer instead of a series of single pointers can't go well.

I don't know how to properly deal with unknown-but-static-length arrays in rust, but if you change the extern declaration to const boolnames: [*c_char * 1]; and then say let mut p: **c_char = ptr::to_unsafe_ptr(&boolnames[0]);, it'll print a pile of stuff and not segfault.

@eevee
Copy link
Author

eevee commented Nov 2, 2012

Ah, you're right, I fell into a common C pitfall. (Doesn't help that curses has another definition that is char** but that gets #ifdefed out.)

Definitely not Rust's fault, then. Thanks :)

@eevee eevee closed this as completed Nov 2, 2012
bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation
Projects
None yet
Development

No branches or pull requests

2 participants