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

Lint null function pointers #1644

Closed
oli-obk opened this issue Mar 30, 2017 · 6 comments · Fixed by #10099
Closed

Lint null function pointers #1644

oli-obk opened this issue Mar 30, 2017 · 6 comments · Fixed by #10099
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group T-middle Type: Probably requires verifiying types

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Mar 30, 2017

rust-lang/rust#40913 showed a real world case of assuming that fn() can be null.

patterns that should be linted are

std::mem::transmute::<Anything, SomeFnPtrType>(std::ptr::null())
(fn_ptr as *mut something).is_null()
@oli-obk oli-obk added L-correctness Lint: Belongs in the correctness lint group good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints T-middle Type: Probably requires verifiying types labels Mar 30, 2017
@rillian
Copy link

rillian commented Mar 30, 2017

I think also

(fn_ptr as *const something).is_null()

And perhaps suggest as Option<fn_ptr> instead.

I guess we can't lint a bare fn_ptr in a #[repr(C)] struct because if it's only created on the Rust side, C code could read it safely relying on it being non-null.

@Manishearth
Copy link
Member

I don't see why you want to cast to a *const Option<fn>?

@rillian
Copy link

rillian commented Mar 30, 2017

I don't properly understand the difference between *mut and *const with function pointers. (fn_ptr as *const something) compiles, so I figured the lint should take both into account.

@Manishearth
Copy link
Member

Sure, my issue is with the Option being beind a pointer. You probably want to transmute the C void pointer to an Option<fn>, not *Option<fn>

@rillian
Copy link

rillian commented Mar 30, 2017

Oh, right. Sorry! Yes that. :) I'll edit for clarity.

@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 31, 2017

I would not transmute (cast is not possible anyway) to Option<fn()>, since that gives the wrong idea. You need to do Some(fn_ptr) if you want a nullable fn pointer.

Anyway, this is a clear bug-case, so we should not suggest anything, since that will totally depend on the use case and it will be incredibly hard to create a good suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-correctness Lint: Belongs in the correctness lint group T-middle Type: Probably requires verifiying types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants