-
Notifications
You must be signed in to change notification settings - Fork 1.7k
add from_raw_with_void_ptr
lint
#9690
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use clippy_utils::path_def_id; | ||
use clippy_utils::ty::is_c_void; | ||
use rustc_hir::{Expr, ExprKind, QPath}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::RawPtr; | ||
use rustc_middle::ty::TypeAndMut; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks if we're passing a `c_void` raw pointer to `Box::from_raw(_)` | ||
/// | ||
/// ### Why is this bad? | ||
/// However, it is easy to run into the pitfall of calling from_raw with the c_void pointer. | ||
/// Note that the definition of, say, Box::from_raw is: | ||
/// | ||
/// `pub unsafe fn from_raw(raw: *mut T) -> Box<T>` | ||
/// | ||
/// meaning that if you pass a *mut c_void you will get a Box<c_void>. | ||
/// Per the safety requirements in the documentation, for this to be safe, | ||
/// c_void would need to have the same memory layout as the original type, which is often not the case. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// # use std::ffi::c_void; | ||
/// let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; | ||
/// let _ = unsafe { Box::from_raw(ptr) }; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// # use std::ffi::c_void; | ||
/// # let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; | ||
/// let _ = unsafe { Box::from_raw(ptr as *mut usize) }; | ||
/// ``` | ||
/// | ||
#[clippy::version = "1.66.0"] | ||
pub FROM_RAW_WITH_VOID_PTR, | ||
suspicious, | ||
"creating a `Box` from a raw void pointer" | ||
} | ||
declare_lint_pass!(FromRawWithVoidPtr => [FROM_RAW_WITH_VOID_PTR]); | ||
|
||
impl LateLintPass<'_> for FromRawWithVoidPtr { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let ExprKind::Call(box_from_raw, [arg]) = expr.kind | ||
&& let ExprKind::Path(QPath::TypeRelative(ty, seg)) = box_from_raw.kind | ||
&& seg.ident.name == sym!(from_raw) | ||
// FIXME: This lint is also applicable to other types, like `Rc`, `Arc` and `Weak`. | ||
&& path_def_id(cx, ty).map_or(false, |id| Some(id) == cx.tcx.lang_items().owned_box()) | ||
&& let arg_kind = cx.typeck_results().expr_ty(arg).kind() | ||
&& let RawPtr(TypeAndMut { ty, .. }) = arg_kind | ||
&& is_c_void(cx, *ty) { | ||
span_lint_and_help(cx, FROM_RAW_WITH_VOID_PTR, expr.span, "creating a `Box` from a raw void pointer", Some(arg.span), "cast this to a pointer of the actual type"); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
### What it does | ||
Checks if we're passing a `c_void` raw pointer to `Box::from_raw(_)` | ||
|
||
### Why is this bad? | ||
However, it is easy to run into the pitfall of calling from_raw with the c_void pointer. | ||
Note that the definition of, say, Box::from_raw is: | ||
|
||
`pub unsafe fn from_raw(raw: *mut T) -> Box<T>` | ||
|
||
meaning that if you pass a *mut c_void you will get a Box<c_void>. | ||
Per the safety requirements in the documentation, for this to be safe, | ||
c_void would need to have the same memory layout as the original type, which is often not the case. | ||
|
||
### Example | ||
``` | ||
let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; | ||
let _ = unsafe { Box::from_raw(ptr) }; | ||
``` | ||
Use instead: | ||
``` | ||
let _ = unsafe { Box::from_raw(ptr as *mut usize) }; | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![warn(clippy::from_raw_with_void_ptr)] | ||
|
||
use std::ffi::c_void; | ||
|
||
fn main() { | ||
// must lint | ||
let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void; | ||
let _ = unsafe { Box::from_raw(ptr) }; | ||
|
||
// shouldn't be linted | ||
let _ = unsafe { Box::from_raw(ptr as *mut usize) }; | ||
|
||
// shouldn't be linted | ||
let should_not_lint_ptr = Box::into_raw(Box::new(12u8)) as *mut u8; | ||
let _ = unsafe { Box::from_raw(should_not_lint_ptr as *mut u8) }; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: creating a `Box` from a raw void pointer | ||
--> $DIR/from_raw_with_void_ptr.rs:8:22 | ||
| | ||
LL | let _ = unsafe { Box::from_raw(ptr) }; | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: cast this to a pointer of the actual type | ||
--> $DIR/from_raw_with_void_ptr.rs:8:36 | ||
| | ||
LL | let _ = unsafe { Box::from_raw(ptr) }; | ||
| ^^^ | ||
= note: `-D clippy::from-raw-with-void-ptr` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.