2021-10-07 15:53:08 +00:00
|
|
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
|
|
|
|
use clippy_utils::higher::{get_vec_init_kind, VecInitKind};
|
2021-10-05 15:43:44 +00:00
|
|
|
use clippy_utils::ty::{is_type_diagnostic_item, is_uninit_value_valid_for_ty};
|
|
|
|
use clippy_utils::{is_lint_allowed, path_to_local_id, peel_hir_expr_while, SpanlessEq};
|
2021-10-03 04:23:57 +00:00
|
|
|
use rustc_hir::{Block, Expr, ExprKind, HirId, PatKind, PathSegment, Stmt, StmtKind};
|
2021-09-17 06:55:26 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-10-05 15:43:44 +00:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2021-09-17 06:55:26 +00:00
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-09-17 18:42:32 +00:00
|
|
|
use rustc_span::{sym, Span};
|
2021-09-17 06:55:26 +00:00
|
|
|
|
2021-10-03 04:23:57 +00:00
|
|
|
// TODO: add `ReadBuf` (RFC 2930) in "How to fix" once it is available in std
|
2021-09-17 06:55:26 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2021-10-03 04:23:57 +00:00
|
|
|
/// Checks for `set_len()` call that creates `Vec` with uninitialized elements.
|
|
|
|
/// This is commonly caused by calling `set_len()` right after after calling
|
|
|
|
/// `with_capacity()` or `reserve()`.
|
2021-09-17 06:55:26 +00:00
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
2021-10-05 15:43:44 +00:00
|
|
|
/// It creates a `Vec` with uninitialized data, which leads to
|
2021-09-17 06:55:26 +00:00
|
|
|
/// undefined behavior with most safe operations.
|
2021-10-05 15:43:44 +00:00
|
|
|
///
|
2021-10-03 04:23:57 +00:00
|
|
|
/// Notably, uninitialized `Vec<u8>` must not be used with generic `Read`.
|
2021-09-17 06:55:26 +00:00
|
|
|
///
|
2021-10-05 15:43:44 +00:00
|
|
|
/// ### Known Problems
|
|
|
|
/// This lint only checks directly adjacent statements.
|
|
|
|
///
|
2021-09-17 06:55:26 +00:00
|
|
|
/// ### Example
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let mut vec: Vec<u8> = Vec::with_capacity(1000);
|
|
|
|
/// unsafe { vec.set_len(1000); }
|
|
|
|
/// reader.read(&mut vec); // undefined behavior!
|
|
|
|
/// ```
|
2021-10-03 04:23:57 +00:00
|
|
|
///
|
|
|
|
/// ### How to fix?
|
|
|
|
/// 1. Use an initialized buffer:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let mut vec: Vec<u8> = vec![0; 1000];
|
|
|
|
/// reader.read(&mut vec);
|
|
|
|
/// ```
|
|
|
|
/// 2. Wrap the content in `MaybeUninit`:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let mut vec: Vec<MaybeUninit<T>> = Vec::with_capacity(1000);
|
|
|
|
/// vec.set_len(1000); // `MaybeUninit` can be uninitialized
|
|
|
|
/// ```
|
|
|
|
/// 3. If you are on nightly, `Vec::spare_capacity_mut()` is available:
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let mut vec: Vec<u8> = Vec::with_capacity(1000);
|
|
|
|
/// let remaining = vec.spare_capacity_mut(); // `&mut [MaybeUninit<u8>]`
|
|
|
|
/// // perform initialization with `remaining`
|
|
|
|
/// vec.set_len(...); // Safe to call `set_len()` on initialized part
|
|
|
|
/// ```
|
2021-09-17 06:55:26 +00:00
|
|
|
pub UNINIT_VEC,
|
|
|
|
correctness,
|
|
|
|
"Vec with uninitialized data"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(UninitVec => [UNINIT_VEC]);
|
|
|
|
|
2021-10-05 15:43:44 +00:00
|
|
|
// FIXME: update to a visitor-based implementation.
|
|
|
|
// Threads: https://github.com/rust-lang/rust-clippy/pull/7682#discussion_r710998368
|
2021-09-17 06:55:26 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UninitVec {
|
|
|
|
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
|
2021-10-05 15:43:44 +00:00
|
|
|
if !in_external_macro(cx.tcx.sess, block.span) {
|
|
|
|
for w in block.stmts.windows(2) {
|
|
|
|
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = w[1].kind {
|
|
|
|
handle_uninit_vec_pair(cx, &w[0], expr);
|
|
|
|
}
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 15:43:44 +00:00
|
|
|
if let (Some(stmt), Some(expr)) = (block.stmts.last(), block.expr) {
|
|
|
|
handle_uninit_vec_pair(cx, stmt, expr);
|
|
|
|
}
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:42:32 +00:00
|
|
|
fn handle_uninit_vec_pair(
|
|
|
|
cx: &LateContext<'tcx>,
|
2021-10-03 04:23:57 +00:00
|
|
|
maybe_init_or_reserve: &'tcx Stmt<'tcx>,
|
2021-09-17 18:42:32 +00:00
|
|
|
maybe_set_len: &'tcx Expr<'tcx>,
|
|
|
|
) {
|
2021-09-17 06:55:26 +00:00
|
|
|
if_chain! {
|
2021-10-03 04:23:57 +00:00
|
|
|
if let Some(vec) = extract_init_or_reserve_target(cx, maybe_init_or_reserve);
|
2021-09-17 18:42:32 +00:00
|
|
|
if let Some((set_len_self, call_span)) = extract_set_len_self(cx, maybe_set_len);
|
2021-10-07 15:53:08 +00:00
|
|
|
if vec.location.eq_expr(cx, set_len_self);
|
2021-09-17 06:55:26 +00:00
|
|
|
if let ty::Ref(_, vec_ty, _) = cx.typeck_results().expr_ty_adjusted(set_len_self).kind();
|
|
|
|
if let ty::Adt(_, substs) = vec_ty.kind();
|
2021-10-05 15:43:44 +00:00
|
|
|
// `#[allow(...)]` attribute can be set on enclosing unsafe block of `set_len()`
|
|
|
|
if !is_lint_allowed(cx, UNINIT_VEC, maybe_set_len.hir_id);
|
2021-09-17 06:55:26 +00:00
|
|
|
then {
|
2021-10-07 15:53:08 +00:00
|
|
|
if vec.has_capacity() {
|
|
|
|
// with_capacity / reserve -> set_len
|
|
|
|
|
|
|
|
// Check T of Vec<T>
|
|
|
|
if !is_uninit_value_valid_for_ty(cx, substs.type_at(0)) {
|
|
|
|
// FIXME: #7698, false positive of the internal lints
|
|
|
|
#[allow(clippy::collapsible_span_lint_calls)]
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
UNINIT_VEC,
|
|
|
|
vec![call_span, maybe_init_or_reserve.span],
|
|
|
|
"calling `set_len()` immediately after reserving a buffer creates uninitialized values",
|
|
|
|
|diag| {
|
|
|
|
diag.help("initialize the buffer or wrap the content in `MaybeUninit`");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// new / default -> set_len
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNINIT_VEC,
|
|
|
|
vec![call_span, maybe_init_or_reserve.span],
|
|
|
|
"calling `set_len()` on empty `Vec` creates out-of-bound values",
|
2021-10-09 09:59:53 +00:00
|
|
|
);
|
2021-10-07 15:53:08 +00:00
|
|
|
}
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:53:08 +00:00
|
|
|
/// The target `Vec` that is initialized or reserved
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct TargetVec<'tcx> {
|
|
|
|
location: VecLocation<'tcx>,
|
|
|
|
/// `None` if `reserve()`
|
|
|
|
init_kind: Option<VecInitKind>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TargetVec<'_> {
|
|
|
|
pub fn has_capacity(self) -> bool {
|
|
|
|
!matches!(self.init_kind, Some(VecInitKind::New | VecInitKind::Default))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum VecLocation<'tcx> {
|
2021-09-17 06:55:26 +00:00
|
|
|
Local(HirId),
|
|
|
|
Expr(&'tcx Expr<'tcx>),
|
|
|
|
}
|
|
|
|
|
2021-10-07 15:53:08 +00:00
|
|
|
impl<'tcx> VecLocation<'tcx> {
|
|
|
|
pub fn eq_expr(self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
|
2021-09-17 06:55:26 +00:00
|
|
|
match self {
|
2021-10-07 15:53:08 +00:00
|
|
|
VecLocation::Local(hir_id) => path_to_local_id(expr, hir_id),
|
|
|
|
VecLocation::Expr(self_expr) => SpanlessEq::new(cx).eq_expr(self_expr, expr),
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 04:23:57 +00:00
|
|
|
/// Finds the target location where the result of `Vec` initialization is stored
|
|
|
|
/// or `self` expression for `Vec::reserve()`.
|
2021-10-07 15:53:08 +00:00
|
|
|
fn extract_init_or_reserve_target<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) -> Option<TargetVec<'tcx>> {
|
2021-09-17 06:55:26 +00:00
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Local(local) => {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(init_expr) = local.init;
|
|
|
|
if let PatKind::Binding(_, hir_id, _, None) = local.pat.kind;
|
2021-10-07 15:53:08 +00:00
|
|
|
if let Some(init_kind) = get_vec_init_kind(cx, init_expr);
|
2021-09-17 06:55:26 +00:00
|
|
|
then {
|
2021-10-07 15:53:08 +00:00
|
|
|
return Some(TargetVec {
|
|
|
|
location: VecLocation::Local(hir_id),
|
|
|
|
init_kind: Some(init_kind),
|
|
|
|
})
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-10-03 04:23:57 +00:00
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
|
2021-10-07 15:53:08 +00:00
|
|
|
ExprKind::Assign(lhs, rhs, _span) => {
|
|
|
|
if let Some(init_kind) = get_vec_init_kind(cx, rhs) {
|
|
|
|
return Some(TargetVec {
|
|
|
|
location: VecLocation::Expr(lhs),
|
|
|
|
init_kind: Some(init_kind),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2021-10-03 04:23:57 +00:00
|
|
|
ExprKind::MethodCall(path, _, [self_expr, _], _) if is_reserve(cx, path, self_expr) => {
|
2021-10-07 15:53:08 +00:00
|
|
|
return Some(TargetVec {
|
|
|
|
location: VecLocation::Expr(self_expr),
|
|
|
|
init_kind: None,
|
|
|
|
});
|
2021-10-03 04:23:57 +00:00
|
|
|
},
|
2021-10-07 15:53:08 +00:00
|
|
|
_ => (),
|
2021-09-17 06:55:26 +00:00
|
|
|
},
|
2021-10-07 15:53:08 +00:00
|
|
|
StmtKind::Item(_) => (),
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
2021-10-07 15:53:08 +00:00
|
|
|
None
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 04:23:57 +00:00
|
|
|
fn is_reserve(cx: &LateContext<'_>, path: &PathSegment<'_>, self_expr: &Expr<'_>) -> bool {
|
|
|
|
is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(self_expr).peel_refs(), sym::Vec)
|
|
|
|
&& path.ident.name.as_str() == "reserve"
|
2021-09-17 06:55:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 07:27:31 +00:00
|
|
|
/// Returns self if the expression is `Vec::set_len()`
|
2021-09-17 06:55:26 +00:00
|
|
|
fn extract_set_len_self(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Option<(&'tcx Expr<'tcx>, Span)> {
|
|
|
|
// peel unsafe blocks in `unsafe { vec.set_len() }`
|
|
|
|
let expr = peel_hir_expr_while(expr, |e| {
|
|
|
|
if let ExprKind::Block(block, _) = e.kind {
|
2021-09-20 19:32:53 +00:00
|
|
|
// Extract the first statement/expression
|
2021-09-17 06:55:26 +00:00
|
|
|
match (block.stmts.get(0).map(|stmt| &stmt.kind), block.expr) {
|
|
|
|
(None, Some(expr)) => Some(expr),
|
2021-09-20 19:32:53 +00:00
|
|
|
(Some(StmtKind::Expr(expr) | StmtKind::Semi(expr)), _) => Some(expr),
|
2021-09-17 06:55:26 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
match expr.kind {
|
2021-10-03 04:23:57 +00:00
|
|
|
ExprKind::MethodCall(path, _, [self_expr, _], _) => {
|
|
|
|
let self_type = cx.typeck_results().expr_ty(self_expr).peel_refs();
|
|
|
|
if is_type_diagnostic_item(cx, self_type, sym::Vec) && path.ident.name.as_str() == "set_len" {
|
|
|
|
Some((self_expr, expr.span))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-09-17 06:55:26 +00:00
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|