forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#9247 - clubby789:raw_slice_pointer_cast, r=Al…
…exendoo New lint: Raw slice pointer cast Adds a lint to check for a raw slice being created and cast back to a pointer, suggesting `ptr::slice_from_raw_parts`, to identify UB such as thomcc/rust-typed-arena#54. ``` changelog: [`cast_slice_from_raw_parts`]: Add lint to check for `slice::from_raw_parts(.., ..) as *const _` ```
- Loading branch information
Showing
9 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,63 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::{match_def_path, meets_msrv, msrvs, paths}; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{def_id::DefId, Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self, Ty}; | ||
use rustc_semver::RustcVersion; | ||
|
||
use super::CAST_SLICE_FROM_RAW_PARTS; | ||
|
||
enum RawPartsKind { | ||
Immutable, | ||
Mutable, | ||
} | ||
|
||
fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option<RawPartsKind> { | ||
if match_def_path(cx, did, &paths::SLICE_FROM_RAW_PARTS) { | ||
Some(RawPartsKind::Immutable) | ||
} else if match_def_path(cx, did, &paths::SLICE_FROM_RAW_PARTS_MUT) { | ||
Some(RawPartsKind::Mutable) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub(super) fn check( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
cast_expr: &Expr<'_>, | ||
cast_to: Ty<'_>, | ||
msrv: Option<RustcVersion>, | ||
) { | ||
if_chain! { | ||
if meets_msrv(msrv, msrvs::PTR_SLICE_RAW_PARTS); | ||
if let ty::RawPtr(ptrty) = cast_to.kind(); | ||
if let ty::Slice(_) = ptrty.ty.kind(); | ||
if let ExprKind::Call(fun, [ptr_arg, len_arg]) = cast_expr.peel_blocks().kind; | ||
if let ExprKind::Path(ref qpath) = fun.kind; | ||
if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id(); | ||
if let Some(rpk) = raw_parts_kind(cx, fun_def_id); | ||
then { | ||
let func = match rpk { | ||
RawPartsKind::Immutable => "from_raw_parts", | ||
RawPartsKind::Mutable => "from_raw_parts_mut" | ||
}; | ||
let span = expr.span; | ||
let mut applicability = Applicability::MachineApplicable; | ||
let ptr = snippet_with_applicability(cx, ptr_arg.span, "ptr", &mut applicability); | ||
let len = snippet_with_applicability(cx, len_arg.span, "len", &mut applicability); | ||
span_lint_and_sugg( | ||
cx, | ||
CAST_SLICE_FROM_RAW_PARTS, | ||
span, | ||
&format!("casting the result of `{func}` to {cast_to}"), | ||
"replace with", | ||
format!("core::ptr::slice_{func}({ptr}, {len})"), | ||
applicability | ||
); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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,24 @@ | ||
// run-rustfix | ||
#![warn(clippy::cast_slice_from_raw_parts)] | ||
|
||
#[allow(unused_imports, unused_unsafe)] | ||
fn main() { | ||
let mut vec = vec![0u8; 1]; | ||
let ptr: *const u8 = vec.as_ptr(); | ||
let mptr = vec.as_mut_ptr(); | ||
let _: *const [u8] = unsafe { core::ptr::slice_from_raw_parts(ptr, 1) }; | ||
let _: *const [u8] = unsafe { core::ptr::slice_from_raw_parts_mut(mptr, 1) }; | ||
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1); | ||
{ | ||
use core::slice; | ||
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1); | ||
use slice as one; | ||
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1); | ||
} | ||
{ | ||
use std::slice; | ||
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1); | ||
use slice as one; | ||
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1); | ||
} | ||
} |
This file contains 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,24 @@ | ||
// run-rustfix | ||
#![warn(clippy::cast_slice_from_raw_parts)] | ||
|
||
#[allow(unused_imports, unused_unsafe)] | ||
fn main() { | ||
let mut vec = vec![0u8; 1]; | ||
let ptr: *const u8 = vec.as_ptr(); | ||
let mptr = vec.as_mut_ptr(); | ||
let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) as *const [u8] }; | ||
let _: *const [u8] = unsafe { std::slice::from_raw_parts_mut(mptr, 1) as *mut [u8] }; | ||
let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) } as *const [u8]; | ||
{ | ||
use core::slice; | ||
let _: *const [u8] = unsafe { slice::from_raw_parts(ptr, 1) } as *const [u8]; | ||
use slice as one; | ||
let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8]; | ||
} | ||
{ | ||
use std::slice; | ||
let _: *const [u8] = unsafe { slice::from_raw_parts(ptr, 1) } as *const [u8]; | ||
use slice as one; | ||
let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8]; | ||
} | ||
} |
This file contains 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,46 @@ | ||
error: casting the result of `from_raw_parts` to *const [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:9:35 | ||
| | ||
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) as *const [u8] }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)` | ||
| | ||
= note: `-D clippy::cast-slice-from-raw-parts` implied by `-D warnings` | ||
|
||
error: casting the result of `from_raw_parts_mut` to *mut [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:10:35 | ||
| | ||
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts_mut(mptr, 1) as *mut [u8] }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts_mut(mptr, 1)` | ||
|
||
error: casting the result of `from_raw_parts` to *const [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:11:26 | ||
| | ||
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) } as *const [u8]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)` | ||
|
||
error: casting the result of `from_raw_parts` to *const [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:14:30 | ||
| | ||
LL | let _: *const [u8] = unsafe { slice::from_raw_parts(ptr, 1) } as *const [u8]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)` | ||
|
||
error: casting the result of `from_raw_parts` to *const [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:16:30 | ||
| | ||
LL | let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)` | ||
|
||
error: casting the result of `from_raw_parts` to *const [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:20:30 | ||
| | ||
LL | let _: *const [u8] = unsafe { slice::from_raw_parts(ptr, 1) } as *const [u8]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)` | ||
|
||
error: casting the result of `from_raw_parts` to *const [u8] | ||
--> $DIR/cast_raw_slice_pointer_cast.rs:22:30 | ||
| | ||
LL | let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)` | ||
|
||
error: aborting due to 7 previous errors | ||
|