|
| 1 | +use rustc_hir::{Expr, ExprKind, TyKind}; |
| 2 | +use rustc_lint::{LateContext, LateLintPass}; |
| 3 | +use rustc_session::declare_lint_pass; |
| 4 | + |
| 5 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 6 | +use clippy_utils::sym; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// |
| 11 | + /// This lint triggers if a pointer to a Rust `str` is passed into an `extern "C"` interface |
| 12 | + /// where you should instead be providing a pointer to a `CString`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// |
| 16 | + /// Foreign functions under the C ABI expect that a string ends with a null byte (`'\0'`). |
| 17 | + /// Rust's `str` doesn't provide a null byte. Instead it contains a length for the string. |
| 18 | + /// This leads to two problems. |
| 19 | + /// |
| 20 | + /// 1. The length parameter of the Rust string will be misinterpreted as a character, which is logically invalid. |
| 21 | + /// 2. Without a null byte, foreign functions will read beyond the memory allocated to the string searching for the null terminator, causing undefined behavior (UB). |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```no_run |
| 25 | + /// # unsafe extern "C" fn strlen(s: *const i8) -> usize { unimplemented!() } |
| 26 | + /// unsafe { strlen("Hello".as_ptr() as *const _) }; |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```no_run |
| 30 | + /// # unsafe extern "C" fn strlen(s: *const i8) -> usize { unimplemented!() } |
| 31 | + /// let cstring = std::ffi::CString::new("Hello".as_bytes()).unwrap(); |
| 32 | + /// unsafe { strlen(cstring.as_ptr()) }; |
| 33 | + /// ``` |
| 34 | + #[clippy::version = "1.99.0"] |
| 35 | + pub STR_PTR_IN_C_ABI, |
| 36 | + nursery, |
| 37 | + "discourage str pointers in C ABI fns" |
| 38 | +} |
| 39 | + |
| 40 | +declare_lint_pass!(StrPtrInCAbi => [STR_PTR_IN_C_ABI]); |
| 41 | + |
| 42 | +impl<'tcx> LateLintPass<'tcx> for StrPtrInCAbi { |
| 43 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 44 | + // find call expressions |
| 45 | + if let ExprKind::Call(callee, args) = expr.kind |
| 46 | + // where the signature of the callee shows it is |
| 47 | + // extern "C" fn |
| 48 | + && is_extern_c_fn(cx, callee) |
| 49 | + // and takes a raw pointer as an argument |
| 50 | + // and to that argument gives a pointer to a Rust `str` |
| 51 | + && let span = args |
| 52 | + .iter() |
| 53 | + .filter(|arg| is_cast_str_ptr_to_raw_ptr(cx, arg)) |
| 54 | + .map(|arg| arg.span) |
| 55 | + .collect::<Vec<_>>() |
| 56 | + && !span.is_empty() |
| 57 | + { |
| 58 | + span_lint_and_help( |
| 59 | + cx, |
| 60 | + STR_PTR_IN_C_ABI, |
| 61 | + span, |
| 62 | + "giving a pointer to a Rust `str` to an `extern \"C\" fn` can cause undefined behavior", |
| 63 | + /* help_span */ None, |
| 64 | + "first convert the `str` to a `std::ffi::CString` and then get a pointer from there", |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// Does the expression represent an `extern "C" fn` of some type? |
| 71 | +fn is_extern_c_fn<'tcx>(cx: &LateContext<'tcx>, callee: &'tcx Expr<'tcx>) -> bool { |
| 72 | + let callee_ty = cx.typeck_results().expr_ty(callee); |
| 73 | + if !(callee_ty.is_fn() || callee_ty.is_fn_ptr()) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + matches!(callee_ty.fn_sig(cx.tcx).abi(), rustc_abi::ExternAbi::C { .. }) |
| 77 | +} |
| 78 | + |
| 79 | +/// If `arg` is `derefs_to_str.as(_mut)_ptr() as *const(mut) _`, then true. |
| 80 | +fn is_cast_str_ptr_to_raw_ptr<'tcx>(cx: &LateContext<'tcx>, arg: &Expr<'tcx>) -> bool { |
| 81 | + let typeck = cx.typeck_results(); |
| 82 | + if let ExprKind::Cast(expr, ty) = arg.kind |
| 83 | + && matches!(ty.kind, TyKind::Ptr(_)) |
| 84 | + && let ExprKind::MethodCall(method, this, _args, _span) = expr.kind |
| 85 | + && matches!(method.ident.name, sym::as_ptr | sym::as_mut_ptr) |
| 86 | + && typeck.expr_ty_adjusted(this).peel_refs().is_str() |
| 87 | + { |
| 88 | + return true; |
| 89 | + } |
| 90 | + false |
| 91 | +} |
0 commit comments