-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add transmute_slice_to_larger_element_type
lint
#10312
Closed
KisaragiEffective
wants to merge
7
commits into
rust-lang:master
from
KisaragiEffective:feature/transmute-slice-to-larger-element-type
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2d3b145
Add `transmute_slice_to_larger_element_type` lint
KisaragiEffective 9288d7f
add documentation
KisaragiEffective 43eeb82
fixes for align_to issue
KisaragiEffective 222596c
fix diffed stderr
KisaragiEffective 3bf3cdf
delete unnecessary TODO
KisaragiEffective 856a69c
fix call for slice conversion
KisaragiEffective 7c9fe29
add FIXME
KisaragiEffective 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 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
71 changes: 71 additions & 0 deletions
71
clippy_lints/src/transmute/transmute_slice_to_larger_element_type.rs
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,71 @@ | ||
use super::TRANSMUTE_SLICE_TO_LARGER_ELEMENT_TYPE; | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::source::reindent_multiline; | ||
use clippy_utils::sugg; | ||
use clippy_utils::ty::approx_ty_size; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty::{self, Ty}; | ||
use std::borrow::Cow; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
call_to_transmute: &'tcx Expr<'_>, | ||
from_ty: Ty<'tcx>, | ||
to_ty: Ty<'tcx>, | ||
transmute_arg: &'tcx Expr<'_>, | ||
) -> bool { | ||
if let (ty::Ref(_, ty_from, _), ty::Ref(_, ty_to, _)) = (&from_ty.kind(), &to_ty.kind()) { | ||
if let (&ty::Slice(ty_elem_from), &ty::Slice(ty_elem_to)) = (&ty_from.kind(), &ty_to.kind()) { | ||
let ty_eleme_from_size = approx_ty_size(cx, *ty_elem_from); | ||
let ty_elem_to_size = approx_ty_size(cx, *ty_elem_to); | ||
if ty_eleme_from_size < ty_elem_to_size { | ||
// this is UB!! | ||
span_lint_and_then( | ||
cx, | ||
TRANSMUTE_SLICE_TO_LARGER_ELEMENT_TYPE, | ||
call_to_transmute.span, | ||
&format!("transmute from `&[{ty_elem_from}]` to `&[{ty_elem_to}]` results in undefined behavior"), | ||
|diag| { | ||
let transmute_arg = sugg::Sugg::hir(cx, transmute_arg, ".."); | ||
// TODO: In this case, outer unsafe block is not needed anymore. It should be removed in | ||
// suggestion. | ||
// FIXME: this do not compile, because temporal Vec dropped at end of outer unsafe block. | ||
let sugg_reallocate = format!( | ||
"{transmute_arg}\ | ||
.iter()\ | ||
.map(|item| unsafe {{ std::mem::transmute(item) }})\ | ||
.collect::<Vec<_>>()\ | ||
.as_slice()" | ||
); | ||
let sugg_reallocate = Cow::from(sugg_reallocate); | ||
let sugg_align_to = format!("({transmute_arg}).align_to::<{ty_elem_to}>().1"); | ||
let sugg_align_to = Cow::from(sugg_align_to); | ||
diag.note("this transmute leads out-of-bounds read"); | ||
diag.span_suggestions( | ||
call_to_transmute.span, | ||
"try", | ||
[ | ||
reindent_multiline(sugg_reallocate, true, None).to_string(), | ||
// TODO: this suggestion does not check if there's prefix and postfix. | ||
// NOTE: this is not what user want to do if ty_elem_to is ZST; however, | ||
// this lint will not fire in such case anyway (ZSTs cannot be larger than any type). | ||
reindent_multiline(sugg_align_to, true, None).to_string(), | ||
], | ||
Applicability::Unspecified, | ||
); | ||
}, | ||
); | ||
|
||
true | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
} | ||
} |
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,9 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::transmute_slice_to_larger_element_type)] | ||
|
||
fn i8_slice_to_i32_slice() { | ||
let i8_slice: &[i8] = &[1i8, 2, 3, 4]; | ||
let i32_slice: &[i32] = unsafe { std::mem::transmute(i8_slice) }; | ||
} | ||
|
||
fn main() {} |
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,17 @@ | ||
error: transmute from `&[i8]` to `&[i32]` results in undefined behavior | ||
--> $DIR/transmute_slice_to_larger_element_type.rs:6:38 | ||
| | ||
LL | let i32_slice: &[i32] = unsafe { std::mem::transmute(i8_slice) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: this transmute leads out-of-bounds read | ||
= note: `-D clippy::transmute-slice-to-larger-element-type` implied by `-D warnings` | ||
help: try | ||
| | ||
LL | let i32_slice: &[i32] = unsafe { (i8_slice).align_to::<i32>().1 }; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
LL | let i32_slice: &[i32] = unsafe { i8_slice.iter().map(|item| unsafe { std::mem::transmute(item) }).collect::<Vec<_>>().to_slice() }; | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tried this suggestion? I don't think this will do what you want: this attempts to transmute
&i8
toi32
.Also, isn't the vector you create dropped at the end of the statement? (and don't you mean
.as_slice()
?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this code does not compile because of the temporary
Vec
being dropped, and thetransmute
on each element is wrong as well. A "proper" suggestion might be to go throughcore::slice::from_raw_parts
, but also because of alignment requirements, I don't think it can be an automated suggestion. Perhaps "considercore::slice::from_raw_parts(_mut)
instead" would suffice?