Also consider transmute
with the invalid_reference_casting
lint
This commit is contained in:
parent
b03864d546
commit
b517dd5bc9
3 changed files with 74 additions and 33 deletions
|
@ -98,32 +98,56 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
|
||||||
fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
|
fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
|
||||||
let e = e.peel_blocks();
|
let e = e.peel_blocks();
|
||||||
|
|
||||||
// <expr> as *mut ...
|
fn from_casts<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
|
||||||
let e = if let ExprKind::Cast(e, t) = e.kind
|
// <expr> as *mut ...
|
||||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
let e = if let ExprKind::Cast(e, t) = e.kind
|
||||||
e
|
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
||||||
// <expr>.cast_mut()
|
e
|
||||||
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
|
// <expr>.cast_mut()
|
||||||
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
|
||||||
&& cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {
|
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
||||||
expr
|
&& cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {
|
||||||
} else {
|
expr
|
||||||
return false;
|
} else {
|
||||||
};
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
let e = e.peel_blocks();
|
let e = e.peel_blocks();
|
||||||
|
|
||||||
// <expr> as *const ...
|
// <expr> as *const ...
|
||||||
let e = if let ExprKind::Cast(e, t) = e.kind
|
let e = if let ExprKind::Cast(e, t) = e.kind
|
||||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
||||||
e
|
e
|
||||||
// ptr::from_ref(<expr>)
|
// ptr::from_ref(<expr>)
|
||||||
} else if let ExprKind::Call(path, [arg]) = e.kind
|
} else if let ExprKind::Call(path, [arg]) = e.kind
|
||||||
&& let ExprKind::Path(ref qpath) = path.kind
|
&& let ExprKind::Path(ref qpath) = path.kind
|
||||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||||
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
|
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
|
||||||
arg
|
arg
|
||||||
} else {
|
} else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_transmute<'tcx>(
|
||||||
|
cx: &LateContext<'tcx>,
|
||||||
|
e: &'tcx Expr<'tcx>,
|
||||||
|
) -> Option<&'tcx Expr<'tcx>> {
|
||||||
|
// mem::transmute::<_, *mut _>(<expr>)
|
||||||
|
if let ExprKind::Call(path, [arg]) = e.kind
|
||||||
|
&& let ExprKind::Path(ref qpath) = path.kind
|
||||||
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||||
|
&& cx.tcx.is_diagnostic_item(sym::transmute, def_id)
|
||||||
|
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(e.hir_id).kind() {
|
||||||
|
Some(arg)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let Some(e) = from_casts(cx, e).or_else(|| from_transmute(cx, e)) else {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,8 @@ unsafe fn ref_to_mut() {
|
||||||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
||||||
let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
|
let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
|
||||||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
||||||
|
let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
|
||||||
|
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
||||||
|
|
||||||
let deferred = num as *const i32 as *mut i32;
|
let deferred = num as *const i32 as *mut i32;
|
||||||
let _num = &mut *deferred;
|
let _num = &mut *deferred;
|
||||||
|
@ -47,6 +49,9 @@ unsafe fn assign_to_ref() {
|
||||||
//~^ ERROR assigning to `&T` is undefined behavior
|
//~^ ERROR assigning to `&T` is undefined behavior
|
||||||
*(std::ptr::from_ref({ num }) as *mut i32) += 1;
|
*(std::ptr::from_ref({ num }) as *mut i32) += 1;
|
||||||
//~^ ERROR assigning to `&T` is undefined behavior
|
//~^ ERROR assigning to `&T` is undefined behavior
|
||||||
|
*std::mem::transmute::<_, *mut i32>(num) += 1;
|
||||||
|
//~^ ERROR assigning to `&T` is undefined behavior
|
||||||
|
|
||||||
let value = num as *const i32 as *mut i32;
|
let value = num as *const i32 as *mut i32;
|
||||||
*value = 1;
|
*value = 1;
|
||||||
//~^ ERROR assigning to `&T` is undefined behavior
|
//~^ ERROR assigning to `&T` is undefined behavior
|
||||||
|
|
|
@ -37,7 +37,13 @@ LL | let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
|
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:29:16
|
--> $DIR/reference_casting.rs:27:16
|
||||||
|
|
|
||||||
|
LL | let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
|
||||||
|
--> $DIR/reference_casting.rs:31:16
|
||||||
|
|
|
|
||||||
LL | let deferred = num as *const i32 as *mut i32;
|
LL | let deferred = num as *const i32 as *mut i32;
|
||||||
| ----------------------------- casting happend here
|
| ----------------------------- casting happend here
|
||||||
|
@ -45,48 +51,54 @@ LL | let _num = &mut *deferred;
|
||||||
| ^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:38:5
|
--> $DIR/reference_casting.rs:40:5
|
||||||
|
|
|
|
||||||
LL | *(a as *const _ as *mut _) = String::from("Replaced");
|
LL | *(a as *const _ as *mut _) = String::from("Replaced");
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:40:5
|
--> $DIR/reference_casting.rs:42:5
|
||||||
|
|
|
|
||||||
LL | *(a as *const _ as *mut String) += " world";
|
LL | *(a as *const _ as *mut String) += " world";
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:42:5
|
--> $DIR/reference_casting.rs:44:5
|
||||||
|
|
|
|
||||||
LL | *std::ptr::from_ref(num).cast_mut() += 1;
|
LL | *std::ptr::from_ref(num).cast_mut() += 1;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:44:5
|
--> $DIR/reference_casting.rs:46:5
|
||||||
|
|
|
|
||||||
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
|
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:46:5
|
--> $DIR/reference_casting.rs:48:5
|
||||||
|
|
|
|
||||||
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
|
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:48:5
|
--> $DIR/reference_casting.rs:50:5
|
||||||
|
|
|
|
||||||
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
|
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
--> $DIR/reference_casting.rs:51:5
|
--> $DIR/reference_casting.rs:52:5
|
||||||
|
|
|
||||||
|
LL | *std::mem::transmute::<_, *mut i32>(num) += 1;
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||||
|
--> $DIR/reference_casting.rs:56:5
|
||||||
|
|
|
|
||||||
LL | let value = num as *const i32 as *mut i32;
|
LL | let value = num as *const i32 as *mut i32;
|
||||||
| ----------------------------- casting happend here
|
| ----------------------------- casting happend here
|
||||||
LL | *value = 1;
|
LL | *value = 1;
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
|
||||||
error: aborting due to 14 previous errors
|
error: aborting due to 16 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue