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

Warn against passing a pointer derived from a 0 or 1 sized array to from_raw_parts #5591

Open
retep998 opened this issue May 12, 2020 · 2 comments
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group

Comments

@retep998
Copy link
Member

If there is a fixed size array with a length of 0 or 1, and as_ptr() or as_ptr_mut() is called on that array and the result passed to slice::from_raw_parts or slice::from_raw_parts_mut, then emit a lint.

This is almost definitely a sign of UB: rust-lang/unsafe-code-guidelines#134 (comment)

@Shnatsel
Copy link
Member

Shnatsel commented May 13, 2020

This is a somewhat common pattern, as it turns out. Some examples of its use on crates.io: rust-lang/unsafe-code-guidelines#134 (comment)

@flip1995 flip1995 added L-correctness Lint: Belongs in the correctness lint group A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels May 13, 2020
@flip1995 flip1995 reopened this May 13, 2020
@RalfJung
Copy link
Member

If there is a fixed size array with a length of 0 or 1, and as_ptr() or as_ptr_mut() is called on that array and the result passed to slice::from_raw_parts or slice::from_raw_parts_mut, then emit a lint.

As a slight refinement, the lint could also check that the length passed to from_raw_parts is not a constant 0 or 1. (But that seems unlikely.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-correctness Lint: Belongs in the correctness lint group
Projects
None yet
Development

No branches or pull requests

4 participants