2020-08-18 15:25:21 +00:00
|
|
|
use crate::LateContext;
|
|
|
|
use crate::LateLintPass;
|
|
|
|
use crate::LintContext;
|
2022-06-27 14:47:27 +00:00
|
|
|
use rustc_errors::fluent;
|
2020-08-18 21:02:23 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind, PathSegment};
|
2020-08-18 15:25:21 +00:00
|
|
|
use rustc_middle::ty;
|
2020-09-21 20:32:28 +00:00
|
|
|
use rustc_span::{symbol::sym, ExpnKind, Span};
|
2020-08-18 15:25:21 +00:00
|
|
|
|
|
|
|
declare_lint! {
|
2020-09-22 15:20:06 +00:00
|
|
|
/// The `temporary_cstring_as_ptr` lint detects getting the inner pointer of
|
|
|
|
/// a temporary `CString`.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # #![allow(unused)]
|
2020-09-22 16:38:50 +00:00
|
|
|
/// # use std::ffi::CString;
|
2020-09-22 15:20:06 +00:00
|
|
|
/// let c_str = CString::new("foo").unwrap().as_ptr();
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
|
|
|
/// ### Explanation
|
|
|
|
///
|
|
|
|
/// The inner pointer of a `CString` lives only as long as the `CString` it
|
|
|
|
/// points to. Getting the inner pointer of a *temporary* `CString` allows the `CString`
|
|
|
|
/// to be dropped at the end of the statement, as it is not being referenced as far as the typesystem
|
|
|
|
/// is concerned. This means outside of the statement the pointer will point to freed memory, which
|
|
|
|
/// causes undefined behavior if the pointer is later dereferenced.
|
2020-08-18 15:25:21 +00:00
|
|
|
pub TEMPORARY_CSTRING_AS_PTR,
|
2020-08-23 18:21:58 +00:00
|
|
|
Warn,
|
2020-08-18 15:25:21 +00:00
|
|
|
"detects getting the inner pointer of a temporary `CString`"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(TemporaryCStringAsPtr => [TEMPORARY_CSTRING_AS_PTR]);
|
|
|
|
|
|
|
|
fn in_macro(span: Span) -> bool {
|
|
|
|
if span.from_expansion() {
|
|
|
|
!matches!(span.ctxt().outer_expn_data().kind, ExpnKind::Desugaring(..))
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 21:02:23 +00:00
|
|
|
fn first_method_call<'tcx>(
|
|
|
|
expr: &'tcx Expr<'tcx>,
|
2022-09-01 04:27:31 +00:00
|
|
|
) -> Option<(&'tcx PathSegment<'tcx>, &'tcx Expr<'tcx>)> {
|
|
|
|
if let ExprKind::MethodCall(path, receiver, args, ..) = &expr.kind {
|
|
|
|
if args.iter().any(|e| e.span.from_expansion()) || receiver.span.from_expansion() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((path, *receiver))
|
|
|
|
}
|
2020-08-18 21:02:23 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:25:21 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|
|
|
if in_macro(expr.span) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-18 21:02:23 +00:00
|
|
|
match first_method_call(expr) {
|
2022-09-02 13:48:14 +00:00
|
|
|
Some((path, unwrap_arg)) if path.ident.name == sym::as_ptr => {
|
2020-08-18 23:37:50 +00:00
|
|
|
let as_ptr_span = path.ident.span;
|
2020-08-18 21:02:23 +00:00
|
|
|
match first_method_call(unwrap_arg) {
|
2022-09-01 04:27:31 +00:00
|
|
|
Some((path, receiver))
|
2020-08-18 21:02:23 +00:00
|
|
|
if path.ident.name == sym::unwrap || path.ident.name == sym::expect =>
|
|
|
|
{
|
2022-09-02 13:48:14 +00:00
|
|
|
lint_cstring_as_ptr(cx, as_ptr_span, receiver, unwrap_arg);
|
2020-08-18 21:02:23 +00:00
|
|
|
}
|
|
|
|
_ => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return,
|
2020-08-18 15:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_cstring_as_ptr(
|
|
|
|
cx: &LateContext<'_>,
|
2020-08-18 23:37:50 +00:00
|
|
|
as_ptr_span: Span,
|
2020-08-18 15:25:21 +00:00
|
|
|
source: &rustc_hir::Expr<'_>,
|
|
|
|
unwrap: &rustc_hir::Expr<'_>,
|
|
|
|
) {
|
|
|
|
let source_type = cx.typeck_results().expr_ty(source);
|
2020-09-21 20:32:28 +00:00
|
|
|
if let ty::Adt(def, substs) = source_type.kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Result, def.did()) {
|
2020-09-21 20:32:28 +00:00
|
|
|
if let ty::Adt(adt, _) = substs.type_at(0).kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) {
|
2022-09-16 07:01:02 +00:00
|
|
|
cx.struct_span_lint(
|
|
|
|
TEMPORARY_CSTRING_AS_PTR,
|
|
|
|
as_ptr_span,
|
|
|
|
fluent::lint::cstring_ptr,
|
|
|
|
|diag| {
|
|
|
|
diag.span_label(as_ptr_span, fluent::lint::as_ptr_label)
|
|
|
|
.span_label(unwrap.span, fluent::lint::unwrap_label)
|
|
|
|
.note(fluent::lint::note)
|
|
|
|
.help(fluent::lint::help)
|
|
|
|
},
|
|
|
|
);
|
2020-08-18 15:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|