Skip to content

Commit cd6a0c8

Browse files
committed
Fix insufficient logic when searching for the underlying allocation
in the `invalid_reference_casting` lint, when trying to lint on bigger memory layout casts.
1 parent 02f7806 commit cd6a0c8

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

compiler/rustc_lint/src/reference_casting.rs

+7
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ fn is_cast_to_bigger_memory_layout<'tcx>(
199199
let e_alloc = cx.expr_or_init(e);
200200
let e_alloc =
201201
if let ExprKind::AddrOf(_, _, inner_expr) = e_alloc.kind { inner_expr } else { e_alloc };
202+
203+
// if the current expr looks like this `&mut expr[index]` then just looking
204+
// at `expr[index]` won't give us the underlying allocation, so we just skip it
205+
if let ExprKind::Index(..) = e_alloc.kind {
206+
return None;
207+
}
208+
202209
let alloc_ty = cx.typeck_results().node_type(e_alloc.hir_id);
203210

204211
// if we do not find it we bail out, as this may not be UB

tests/ui/lint/reference_casting.rs

+8
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ unsafe fn bigger_layout() {
247247
unsafe fn from_ref(this: &i32) -> &i64 {
248248
&*(this as *const i32 as *const i64)
249249
}
250+
251+
// https://github.com/rust-lang/rust/issues/124685
252+
unsafe fn slice_index(array: &mut [u8], offset: usize) {
253+
let a1 = &mut array[offset];
254+
let a2 = a1 as *mut u8;
255+
let a3 = a2 as *mut u64;
256+
unsafe { *a3 = 3 };
257+
}
250258
}
251259

252260
const RAW_PTR: *mut u8 = 1 as *mut u8;

0 commit comments

Comments
 (0)