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

Fix misaligned transmute lint. #2418

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions clippy_lints/src/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc::hir::*;
use std::borrow::Cow;
use syntax::ast;
use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then,
alignment};
span_note_and_lint, alignment};
use utils::{opt_def_id, sugg};

/// **What it does:** Checks for transmutes that can't ever be correct on any
Expand Down Expand Up @@ -169,21 +169,21 @@ declare_lint! {
"transmutes from an integer to a float"
}

/// **What it does:** Checks for transmutes to a potentially less-aligned type.
/// **What it does:** Checks for transmutes from a pointer to a less-strictly aligned type to a pointer to a more-strictly aligned type.
///
/// **Why is this bad?** This might result in undefined behavior.
/// **Why is this bad?** Dereferencing this pointer would result in undefined behavior.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```rust
/// // u32 is 32-bit aligned; u8 is 8-bit aligned
/// let _: u32 = unsafe { std::mem::transmute([0u8; 4]) };
/// let _: &u32 = unsafe { std::mem::transmute(&[0u8; 4]) };
/// ```
declare_lint! {
pub MISALIGNED_TRANSMUTE,
Warn,
"transmutes to a potentially less-aligned type"
"transmutes which potentially involve alignment-based undefined behavior"
}

pub struct Transmute;
Expand Down Expand Up @@ -220,16 +220,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
e.span,
&format!("transmute from a type (`{}`) to itself", from_ty),
),
_ if alignment(cx, from_ty).map(|a| a.abi())
< alignment(cx, to_ty).map(|a| a.abi())
=> span_lint(
// NOTE: don't need to check ref -> raw nor raw -> ref conversions,
// because those are already checked by USELESS_TRANSMUTE and
// TRANSMUTE_PTR_TO_REF
(&ty::TyRef(_, from_ptr_ty), &ty::TyRef(_, to_ptr_ty))
| (&ty::TyRawPtr(from_ptr_ty), &ty::TyRawPtr(to_ptr_ty))
if alignment(cx, from_ptr_ty.ty)
.and_then(|from| alignment(cx, to_ptr_ty.ty)
.map(|to| to.abi().cmp(&from.abi()))
) == Some(::std::cmp::Ordering::Greater)
=> span_note_and_lint(
cx,
MISALIGNED_TRANSMUTE,
e.span,
&format!(
"transmute from `{}` to a less-aligned type (`{}`)",
from_ty,
to_ty,
"transmute from pointer to `{}` to a more-strictly aligned type (`{}`)",
from_ptr_ty,
to_ptr_ty,
),
e.span,
&format!(
"`{}` has {}-byte alignment; `{}` has {}-byte alignment",
from_ptr_ty,
alignment(cx, from_ptr_ty.ty).unwrap().abi(),
to_ptr_ty,
alignment(cx, to_ptr_ty.ty).unwrap().abi(),
)
),
(&ty::TyRef(_, rty), &ty::TyRawPtr(ptr_ty)) => span_lint_and_then(
Expand Down
8 changes: 5 additions & 3 deletions tests/ui/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,11 @@ fn bytes_to_str(b: &[u8], mb: &mut [u8]) {

#[warn(misaligned_transmute)]
fn misaligned_transmute() {
let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // err
let _: u32 = unsafe { std::mem::transmute(0f32) }; // ok (alignment-wise)
let _: [u8; 4] = unsafe { std::mem::transmute(0u32) }; // ok (alignment-wise)
let _: &u32 = unsafe { std::mem::transmute(&[0u8; 4]) }; // err
let _: *const u32 = unsafe { std::mem::transmute(&[0u8; 4] as *const [u8; 4]) }; // err
let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // ok (not pointers)
let _: &u32 = unsafe { std::mem::transmute(&0f32) }; // ok (alignment-wise)
let _: &[u8; 4] = unsafe { std::mem::transmute(&0u32) }; // ok (alignment-wise)
}

fn main() { }
19 changes: 14 additions & 5 deletions tests/ui/transmute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,22 @@ error: transmute from a `&mut [u8]` to a `&mut str`
140 | let _: &mut str = unsafe { std::mem::transmute(mb) };
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8_mut(mb).unwrap()`

error: transmute from `[u8; 4]` to a less-aligned type (`u32`)
--> $DIR/transmute.rs:145:27
error: transmute from pointer to `[u8; 4]` to a more-strictly aligned type (`u32`)
--> $DIR/transmute.rs:145:28
|
145 | let _: u32 = unsafe { std::mem::transmute([0u8; 4]) }; // err
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
145 | let _: &u32 = unsafe { std::mem::transmute(&[0u8; 4]) }; // err
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D misaligned-transmute` implied by `-D warnings`
= note: `[u8; 4]` has 1-byte alignment; `u32` has 4-byte alignment

error: aborting due to 33 previous errors
error: transmute from pointer to `[u8; 4]` to a more-strictly aligned type (`u32`)
--> $DIR/transmute.rs:146:34
|
146 | let _: *const u32 = unsafe { std::mem::transmute(&[0u8; 4] as *const [u8; 4]) }; // err
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `[u8; 4]` has 1-byte alignment; `u32` has 4-byte alignment

error: aborting due to 34 previous errors