Skip to content

Commit 96a2f69

Browse files
committed
Uplift clippy::invalid_null_ptr_usage as invalid_null_arguments
1 parent a20d2ef commit 96a2f69

File tree

6 files changed

+590
-4
lines changed

6 files changed

+590
-4
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ lint_invalid_nan_comparisons_eq_ne = incorrect NaN comparison, NaN cannot be dir
456456
457457
lint_invalid_nan_comparisons_lt_le_gt_ge = incorrect NaN comparison, NaN is not orderable
458458
459+
lint_invalid_null_arguments = calling this function with a null pointer is undefined behavior, even if the result of the function is unused
460+
.origin = null pointer originates from here
461+
.doc = for more information, visit <https://doc.rust-lang.org/std/ptr/index.html> and <https://doc.rust-lang.org/reference/behavior-considered-undefined.html>
462+
459463
lint_invalid_reference_casting_assign_to_ref = assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
460464
.label = casting happened here
461465

Diff for: compiler/rustc_lint/src/lints.rs

+16
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,22 @@ pub(crate) enum UselessPtrNullChecksDiag<'a> {
609609
FnRet { fn_name: Ident },
610610
}
611611

612+
#[derive(LintDiagnostic)]
613+
pub(crate) enum InvalidNullArgumentsDiag {
614+
#[diag(lint_invalid_null_arguments)]
615+
#[help(lint_doc)]
616+
NullPtrInline {
617+
#[label(lint_origin)]
618+
null_span: Span,
619+
},
620+
#[diag(lint_invalid_null_arguments)]
621+
#[help(lint_doc)]
622+
NullPtrThroughBinding {
623+
#[note(lint_origin)]
624+
null_span: Span,
625+
},
626+
}
627+
612628
// for_loops_over_fallibles.rs
613629
#[derive(LintDiagnostic)]
614630
#[diag(lint_for_loops_over_fallibles)]

Diff for: compiler/rustc_lint/src/ptr_nulls.rs

+103-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use rustc_ast::LitKind;
22
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
3+
use rustc_middle::ty::RawPtr;
34
use rustc_session::{declare_lint, declare_lint_pass};
4-
use rustc_span::sym;
5+
use rustc_span::{Span, sym};
56

6-
use crate::lints::UselessPtrNullChecksDiag;
7+
use crate::lints::{InvalidNullArgumentsDiag, UselessPtrNullChecksDiag};
8+
use crate::utils::peel_casts;
79
use crate::{LateContext, LateLintPass, LintContext};
810

911
declare_lint! {
@@ -31,7 +33,30 @@ declare_lint! {
3133
"useless checking of non-null-typed pointer"
3234
}
3335

34-
declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS]);
36+
declare_lint! {
37+
/// The `invalid_null_arguments` lint checks for invalid usage of null pointers in arguments.
38+
///
39+
/// ### Example
40+
///
41+
/// ```rust,compile_fail
42+
/// # use std::{slice, ptr};
43+
/// // Undefined behavior
44+
/// # let _slice: &[u8] =
45+
/// unsafe { slice::from_raw_parts(ptr::null(), 0) };
46+
/// ```
47+
///
48+
/// {{produces}}
49+
///
50+
/// ### Explanation
51+
///
52+
/// Calling methods whos safety invariants requires non-null ptr with a null pointer
53+
/// is [Undefined Behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html)!
54+
INVALID_NULL_ARGUMENTS,
55+
Deny,
56+
"invalid null pointer in arguments"
57+
}
58+
59+
declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS, INVALID_NULL_ARGUMENTS]);
3560

3661
/// This function checks if the expression is from a series of consecutive casts,
3762
/// ie. `(my_fn as *const _ as *mut _).cast_mut()` and whether the original expression is either
@@ -85,6 +110,25 @@ fn useless_check<'a, 'tcx: 'a>(
85110
}
86111
}
87112

113+
/// Checks if the given expression is a null pointer (modulo casting)
114+
fn is_null_ptr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Span> {
115+
let (expr, _) = peel_casts(cx, expr);
116+
117+
if let ExprKind::Call(path, []) = expr.kind
118+
&& let ExprKind::Path(ref qpath) = path.kind
119+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
120+
&& let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id)
121+
{
122+
(diag_item == sym::ptr_null || diag_item == sym::ptr_null_mut).then_some(expr.span)
123+
} else if let ExprKind::Lit(spanned) = expr.kind
124+
&& let LitKind::Int(v, _) = spanned.node
125+
{
126+
(v == 0).then_some(expr.span)
127+
} else {
128+
None
129+
}
130+
}
131+
88132
impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
89133
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
90134
match expr.kind {
@@ -102,6 +146,62 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
102146
cx.emit_span_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
103147
}
104148

149+
// Catching:
150+
// <path>(arg...) where `arg` is null-ptr and `path` is a fn that expect non-null-ptr
151+
ExprKind::Call(path, args)
152+
if let ExprKind::Path(ref qpath) = path.kind
153+
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
154+
&& let Some(diag_name) = cx.tcx.get_diagnostic_name(def_id) =>
155+
{
156+
// `arg` positions where null would cause U.B and whenever ZST are allowed.
157+
//
158+
// We should probably have a `rustc` attribute, but checking them is costly,
159+
// maybe if we checked for null ptr first, it would be acceptable?
160+
let (arg_indices, are_zsts_allowed): (&[_], _) = match diag_name {
161+
sym::ptr_read
162+
| sym::ptr_read_unaligned
163+
| sym::ptr_read_volatile
164+
| sym::ptr_replace
165+
| sym::ptr_write
166+
| sym::ptr_write_bytes
167+
| sym::ptr_write_unaligned
168+
| sym::ptr_write_volatile => (&[0], true),
169+
sym::slice_from_raw_parts | sym::slice_from_raw_parts_mut => (&[0], false),
170+
sym::ptr_copy
171+
| sym::ptr_copy_nonoverlapping
172+
| sym::ptr_swap
173+
| sym::ptr_swap_nonoverlapping => (&[0, 1], true),
174+
_ => return,
175+
};
176+
177+
for &arg_idx in arg_indices {
178+
if let Some(arg) = args.get(arg_idx)
179+
&& let Some(null_span) = is_null_ptr(cx, arg)
180+
&& let Some(ty) = cx.typeck_results().expr_ty_opt(arg)
181+
&& let RawPtr(ty, _mutbl) = ty.kind()
182+
{
183+
// If ZST are fine, don't lint on them
184+
let typing_env = cx.typing_env();
185+
if are_zsts_allowed
186+
&& cx
187+
.tcx
188+
.layout_of(typing_env.as_query_input(*ty))
189+
.is_ok_and(|layout| layout.is_1zst())
190+
{
191+
break;
192+
}
193+
194+
let diag = if arg.span.contains(null_span) {
195+
InvalidNullArgumentsDiag::NullPtrInline { null_span }
196+
} else {
197+
InvalidNullArgumentsDiag::NullPtrThroughBinding { null_span }
198+
};
199+
200+
cx.emit_span_lint(INVALID_NULL_ARGUMENTS, expr.span, diag)
201+
}
202+
}
203+
}
204+
105205
// Catching:
106206
// (fn_ptr as *<const/mut> <ty>).is_null()
107207
ExprKind::MethodCall(_, receiver, _, _)

Diff for: compiler/rustc_lint/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::LateContext;
66
/// Given an expression, peel all of casts (`<expr> as ...`, `<expr>.cast{,_mut,_const}()`,
77
/// `ptr::from_ref(<expr>)`, ...) and init expressions.
88
///
9-
/// Returns the outermost expression and a boolean representing if one of the casts was
9+
/// Returns the innermost expression and a boolean representing if one of the casts was
1010
/// `UnsafeCell::raw_get(<expr>)`
1111
pub(crate) fn peel_casts<'tcx>(
1212
cx: &LateContext<'tcx>,

Diff for: tests/ui/lint/invalid_null_args.rs

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// check-fail
2+
// run-rustfix
3+
4+
use std::ptr;
5+
use std::mem;
6+
7+
unsafe fn null_ptr() {
8+
ptr::write(
9+
//~^ ERROR calling this function with a null pointer is undefined behavior
10+
ptr::null_mut() as *mut u32,
11+
mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
12+
);
13+
14+
let null_ptr = ptr::null_mut();
15+
ptr::write(
16+
//~^ ERROR calling this function with a null pointer is undefined behavior
17+
null_ptr as *mut u32,
18+
mem::transmute::<[u8; 4], _>([0, 0, 0, 255]),
19+
);
20+
21+
let _: &[usize] = std::slice::from_raw_parts(ptr::null(), 0);
22+
//~^ ERROR calling this function with a null pointer is undefined behavior
23+
let _: &[usize] = std::slice::from_raw_parts(ptr::null_mut(), 0);
24+
//~^ ERROR calling this function with a null pointer is undefined behavior
25+
let _: &[usize] = std::slice::from_raw_parts(0 as *mut _, 0);
26+
//~^ ERROR calling this function with a null pointer is undefined behavior
27+
let _: &[usize] = std::slice::from_raw_parts(mem::transmute(0usize), 0);
28+
//~^ ERROR calling this function with a null pointer is undefined behavior
29+
30+
let _: &[usize] = std::slice::from_raw_parts_mut(ptr::null_mut(), 0);
31+
//~^ ERROR calling this function with a null pointer is undefined behavior
32+
33+
ptr::copy::<usize>(ptr::null(), ptr::NonNull::dangling().as_ptr(), 0);
34+
//~^ ERROR calling this function with a null pointer is undefined behavior
35+
ptr::copy::<usize>(ptr::NonNull::dangling().as_ptr(), ptr::null_mut(), 0);
36+
//~^ ERROR calling this function with a null pointer is undefined behavior
37+
38+
ptr::copy_nonoverlapping::<usize>(ptr::null(), ptr::NonNull::dangling().as_ptr(), 0);
39+
//~^ ERROR calling this function with a null pointer is undefined behavior
40+
ptr::copy_nonoverlapping::<usize>(
41+
//~^ ERROR calling this function with a null pointer is undefined behavior
42+
ptr::NonNull::dangling().as_ptr(),
43+
ptr::null_mut(),
44+
0
45+
);
46+
47+
#[derive(Copy, Clone)]
48+
struct A(usize);
49+
let mut v = A(200);
50+
51+
let _a: A = ptr::read(ptr::null());
52+
//~^ ERROR calling this function with a null pointer is undefined behavior
53+
let _a: A = ptr::read(ptr::null_mut());
54+
//~^ ERROR calling this function with a null pointer is undefined behavior
55+
56+
let _a: A = ptr::read_unaligned(ptr::null());
57+
//~^ ERROR calling this function with a null pointer is undefined behavior
58+
let _a: A = ptr::read_unaligned(ptr::null_mut());
59+
//~^ ERROR calling this function with a null pointer is undefined behavior
60+
61+
let _a: A = ptr::read_volatile(ptr::null());
62+
//~^ ERROR calling this function with a null pointer is undefined behavior
63+
let _a: A = ptr::read_volatile(ptr::null_mut());
64+
//~^ ERROR calling this function with a null pointer is undefined behavior
65+
66+
let _a: A = ptr::replace(ptr::null_mut(), v);
67+
//~^ ERROR calling this function with a null pointer is undefined behavior
68+
69+
ptr::swap::<A>(ptr::null_mut(), &mut v);
70+
//~^ ERROR calling this function with a null pointer is undefined behavior
71+
ptr::swap::<A>(&mut v, ptr::null_mut());
72+
//~^ ERROR calling this function with a null pointer is undefined behavior
73+
74+
ptr::swap_nonoverlapping::<A>(ptr::null_mut(), &mut v, 0);
75+
//~^ ERROR calling this function with a null pointer is undefined behavior
76+
ptr::swap_nonoverlapping::<A>(&mut v, ptr::null_mut(), 0);
77+
//~^ ERROR calling this function with a null pointer is undefined behavior
78+
79+
ptr::write(ptr::null_mut(), v);
80+
//~^ ERROR calling this function with a null pointer is undefined behavior
81+
82+
ptr::write_unaligned(ptr::null_mut(), v);
83+
//~^ ERROR calling this function with a null pointer is undefined behavior
84+
85+
ptr::write_volatile(ptr::null_mut(), v);
86+
//~^ ERROR calling this function with a null pointer is undefined behavior
87+
88+
ptr::write_bytes::<usize>(ptr::null_mut(), 42, 0);
89+
//~^ ERROR calling this function with a null pointer is undefined behavior
90+
91+
// with indirections
92+
let const_ptr = null_ptr as *const u8;
93+
let _a: u8 = ptr::read(const_ptr);
94+
//~^ ERROR calling this function with a null pointer is undefined behavior
95+
}
96+
97+
unsafe fn zst() {
98+
struct Zst; // zero-sized type
99+
100+
std::slice::from_raw_parts::<()>(ptr::null(), 0);
101+
//~^ ERROR calling this function with a null pointer is undefined behavior
102+
std::slice::from_raw_parts::<Zst>(ptr::null(), 0);
103+
//~^ ERROR calling this function with a null pointer is undefined behavior
104+
std::slice::from_raw_parts_mut::<()>(ptr::null_mut(), 0);
105+
//~^ ERROR calling this function with a null pointer is undefined behavior
106+
std::slice::from_raw_parts_mut::<Zst>(ptr::null_mut(), 0);
107+
//~^ ERROR calling this function with a null pointer is undefined behavior
108+
109+
ptr::read::<()>(ptr::null());
110+
ptr::read::<Zst>(ptr::null());
111+
112+
ptr::write(ptr::null_mut(), ());
113+
ptr::write(ptr::null_mut(), Zst);
114+
115+
ptr::copy(ptr::null::<()>(), ptr::null_mut::<()>(), 1);
116+
ptr::copy(ptr::null::<Zst>(), ptr::null_mut::<Zst>(), 1);
117+
}
118+
119+
unsafe fn not_invalid() {
120+
// Simplified false-positive from std quicksort implementation
121+
122+
let mut a = ptr::null_mut();
123+
let mut b = ();
124+
125+
loop {
126+
if false {
127+
break;
128+
}
129+
130+
a = &raw mut b;
131+
}
132+
133+
ptr::write(a, ());
134+
}
135+
136+
fn main() {}

0 commit comments

Comments
 (0)