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 insufficient logic when searching for the underlying allocation #124761

Merged
merged 1 commit into from
May 8, 2024
Merged
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
7 changes: 7 additions & 0 deletions compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ fn is_cast_to_bigger_memory_layout<'tcx>(
let e_alloc = cx.expr_or_init(e);
let e_alloc =
if let ExprKind::AddrOf(_, _, inner_expr) = e_alloc.kind { inner_expr } else { e_alloc };

// if the current expr looks like this `&mut expr[index]` then just looking
// at `expr[index]` won't give us the underlying allocation, so we just skip it
if let ExprKind::Index(..) = e_alloc.kind {
return None;
}

let alloc_ty = cx.typeck_results().node_type(e_alloc.hir_id);

// if we do not find it we bail out, as this may not be UB
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ unsafe fn bigger_layout() {
unsafe fn from_ref(this: &i32) -> &i64 {
&*(this as *const i32 as *const i64)
}

// https://github.com/rust-lang/rust/issues/124685
unsafe fn slice_index(array: &mut [u8], offset: usize) {
let a1 = &mut array[offset];
let a2 = a1 as *mut u8;
let a3 = a2 as *mut u64;
unsafe { *a3 = 3 };
}
}

const RAW_PTR: *mut u8 = 1 as *mut u8;
Expand Down
Loading