2022-09-05 19:37:30 +00:00
|
|
|
use crate::lints::CStringPtr;
|
2020-08-18 15:25:21 +00:00
|
|
|
use crate::LateContext;
|
|
|
|
use crate::LateLintPass;
|
|
|
|
use crate::LintContext;
|
2023-04-04 18:31:25 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-08-18 15:25:21 +00:00
|
|
|
use rustc_middle::ty;
|
2024-04-29 03:42:13 +00:00
|
|
|
use rustc_session::{declare_lint, declare_lint_pass};
|
2023-04-04 18:31:25 +00:00
|
|
|
use rustc_span::{symbol::sym, 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`
|
2024-05-07 04:12:37 +00:00
|
|
|
/// 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]);
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for TemporaryCStringAsPtr {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2023-04-04 18:31:25 +00:00
|
|
|
if let ExprKind::MethodCall(as_ptr_path, as_ptr_receiver, ..) = expr.kind
|
|
|
|
&& as_ptr_path.ident.name == sym::as_ptr
|
|
|
|
&& let ExprKind::MethodCall(unwrap_path, unwrap_receiver, ..) = as_ptr_receiver.kind
|
|
|
|
&& (unwrap_path.ident.name == sym::unwrap || unwrap_path.ident.name == sym::expect)
|
|
|
|
{
|
|
|
|
lint_cstring_as_ptr(cx, as_ptr_path.ident.span, unwrap_receiver, as_ptr_receiver);
|
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);
|
2023-07-11 21:35:29 +00:00
|
|
|
if let ty::Adt(def, args) = source_type.kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Result, def.did()) {
|
2023-07-11 21:35:29 +00:00
|
|
|
if let ty::Adt(adt, _) = args.type_at(0).kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::cstring_type, adt.did()) {
|
2024-01-16 03:40:39 +00:00
|
|
|
cx.emit_span_lint(
|
2022-09-16 07:01:02 +00:00
|
|
|
TEMPORARY_CSTRING_AS_PTR,
|
|
|
|
as_ptr_span,
|
2022-09-05 19:37:30 +00:00
|
|
|
CStringPtr { as_ptr: as_ptr_span, unwrap: unwrap.span },
|
2022-09-16 07:01:02 +00:00
|
|
|
);
|
2020-08-18 15:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|