Uplift clippy::invalid_null_ptr_usage
as invalid_null_arguments
This commit is contained in:
parent
a20d2ef0d9
commit
96a2f69844
6 changed files with 590 additions and 4 deletions
|
@ -456,6 +456,10 @@ lint_invalid_nan_comparisons_eq_ne = incorrect NaN comparison, NaN cannot be dir
|
|||
|
||||
lint_invalid_nan_comparisons_lt_le_gt_ge = incorrect NaN comparison, NaN is not orderable
|
||||
|
||||
lint_invalid_null_arguments = calling this function with a null pointer is undefined behavior, even if the result of the function is unused
|
||||
.origin = null pointer originates from here
|
||||
.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>
|
||||
|
||||
lint_invalid_reference_casting_assign_to_ref = assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
.label = casting happened here
|
||||
|
||||
|
|
|
@ -609,6 +609,22 @@ pub(crate) enum UselessPtrNullChecksDiag<'a> {
|
|||
FnRet { fn_name: Ident },
|
||||
}
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
pub(crate) enum InvalidNullArgumentsDiag {
|
||||
#[diag(lint_invalid_null_arguments)]
|
||||
#[help(lint_doc)]
|
||||
NullPtrInline {
|
||||
#[label(lint_origin)]
|
||||
null_span: Span,
|
||||
},
|
||||
#[diag(lint_invalid_null_arguments)]
|
||||
#[help(lint_doc)]
|
||||
NullPtrThroughBinding {
|
||||
#[note(lint_origin)]
|
||||
null_span: Span,
|
||||
},
|
||||
}
|
||||
|
||||
// for_loops_over_fallibles.rs
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(lint_for_loops_over_fallibles)]
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
use rustc_ast::LitKind;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
|
||||
use rustc_middle::ty::RawPtr;
|
||||
use rustc_session::{declare_lint, declare_lint_pass};
|
||||
use rustc_span::sym;
|
||||
use rustc_span::{Span, sym};
|
||||
|
||||
use crate::lints::UselessPtrNullChecksDiag;
|
||||
use crate::lints::{InvalidNullArgumentsDiag, UselessPtrNullChecksDiag};
|
||||
use crate::utils::peel_casts;
|
||||
use crate::{LateContext, LateLintPass, LintContext};
|
||||
|
||||
declare_lint! {
|
||||
|
@ -31,7 +33,30 @@ declare_lint! {
|
|||
"useless checking of non-null-typed pointer"
|
||||
}
|
||||
|
||||
declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS]);
|
||||
declare_lint! {
|
||||
/// The `invalid_null_arguments` lint checks for invalid usage of null pointers in arguments.
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// # use std::{slice, ptr};
|
||||
/// // Undefined behavior
|
||||
/// # let _slice: &[u8] =
|
||||
/// unsafe { slice::from_raw_parts(ptr::null(), 0) };
|
||||
/// ```
|
||||
///
|
||||
/// {{produces}}
|
||||
///
|
||||
/// ### Explanation
|
||||
///
|
||||
/// Calling methods whos safety invariants requires non-null ptr with a null pointer
|
||||
/// is [Undefined Behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html)!
|
||||
INVALID_NULL_ARGUMENTS,
|
||||
Deny,
|
||||
"invalid null pointer in arguments"
|
||||
}
|
||||
|
||||
declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS, INVALID_NULL_ARGUMENTS]);
|
||||
|
||||
/// This function checks if the expression is from a series of consecutive casts,
|
||||
/// 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>(
|
|||
}
|
||||
}
|
||||
|
||||
/// Checks if the given expression is a null pointer (modulo casting)
|
||||
fn is_null_ptr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Span> {
|
||||
let (expr, _) = peel_casts(cx, expr);
|
||||
|
||||
if let ExprKind::Call(path, []) = expr.kind
|
||||
&& let ExprKind::Path(ref qpath) = path.kind
|
||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||
&& let Some(diag_item) = cx.tcx.get_diagnostic_name(def_id)
|
||||
{
|
||||
(diag_item == sym::ptr_null || diag_item == sym::ptr_null_mut).then_some(expr.span)
|
||||
} else if let ExprKind::Lit(spanned) = expr.kind
|
||||
&& let LitKind::Int(v, _) = spanned.node
|
||||
{
|
||||
(v == 0).then_some(expr.span)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
match expr.kind {
|
||||
|
@ -102,6 +146,62 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
|
|||
cx.emit_span_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
|
||||
}
|
||||
|
||||
// Catching:
|
||||
// <path>(arg...) where `arg` is null-ptr and `path` is a fn that expect non-null-ptr
|
||||
ExprKind::Call(path, args)
|
||||
if let ExprKind::Path(ref qpath) = path.kind
|
||||
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
||||
&& let Some(diag_name) = cx.tcx.get_diagnostic_name(def_id) =>
|
||||
{
|
||||
// `arg` positions where null would cause U.B and whenever ZST are allowed.
|
||||
//
|
||||
// We should probably have a `rustc` attribute, but checking them is costly,
|
||||
// maybe if we checked for null ptr first, it would be acceptable?
|
||||
let (arg_indices, are_zsts_allowed): (&[_], _) = match diag_name {
|
||||
sym::ptr_read
|
||||
| sym::ptr_read_unaligned
|
||||
| sym::ptr_read_volatile
|
||||
| sym::ptr_replace
|
||||
| sym::ptr_write
|
||||
| sym::ptr_write_bytes
|
||||
| sym::ptr_write_unaligned
|
||||
| sym::ptr_write_volatile => (&[0], true),
|
||||
sym::slice_from_raw_parts | sym::slice_from_raw_parts_mut => (&[0], false),
|
||||
sym::ptr_copy
|
||||
| sym::ptr_copy_nonoverlapping
|
||||
| sym::ptr_swap
|
||||
| sym::ptr_swap_nonoverlapping => (&[0, 1], true),
|
||||
_ => return,
|
||||
};
|
||||
|
||||
for &arg_idx in arg_indices {
|
||||
if let Some(arg) = args.get(arg_idx)
|
||||
&& let Some(null_span) = is_null_ptr(cx, arg)
|
||||
&& let Some(ty) = cx.typeck_results().expr_ty_opt(arg)
|
||||
&& let RawPtr(ty, _mutbl) = ty.kind()
|
||||
{
|
||||
// If ZST are fine, don't lint on them
|
||||
let typing_env = cx.typing_env();
|
||||
if are_zsts_allowed
|
||||
&& cx
|
||||
.tcx
|
||||
.layout_of(typing_env.as_query_input(*ty))
|
||||
.is_ok_and(|layout| layout.is_1zst())
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
let diag = if arg.span.contains(null_span) {
|
||||
InvalidNullArgumentsDiag::NullPtrInline { null_span }
|
||||
} else {
|
||||
InvalidNullArgumentsDiag::NullPtrThroughBinding { null_span }
|
||||
};
|
||||
|
||||
cx.emit_span_lint(INVALID_NULL_ARGUMENTS, expr.span, diag)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Catching:
|
||||
// (fn_ptr as *<const/mut> <ty>).is_null()
|
||||
ExprKind::MethodCall(_, receiver, _, _)
|
||||
|
|
|
@ -6,7 +6,7 @@ use crate::LateContext;
|
|||
/// Given an expression, peel all of casts (`<expr> as ...`, `<expr>.cast{,_mut,_const}()`,
|
||||
/// `ptr::from_ref(<expr>)`, ...) and init expressions.
|
||||
///
|
||||
/// Returns the outermost expression and a boolean representing if one of the casts was
|
||||
/// Returns the innermost expression and a boolean representing if one of the casts was
|
||||
/// `UnsafeCell::raw_get(<expr>)`
|
||||
pub(crate) fn peel_casts<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue