|
| 1 | +mod mem_unsafe_functions; |
| 2 | + |
| 3 | +use rustc_hir as hir; |
| 4 | +use rustc_hir::intravisit; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 7 | +use rustc_span::def_id::LocalDefId; |
| 8 | +use rustc_span::Span; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Check for direct usage of external functions that modify memory |
| 13 | + /// without concerning about memory safety, such as `memcpy`, `strcpy`, `strcat` etc. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// These function can be dangerous when used incorrectly, |
| 17 | + /// which could potentially introduce vulnerablities such as buffer overflow to the software. |
| 18 | + /// |
| 19 | + /// ### Example |
| 20 | + /// ```rust |
| 21 | + /// extern "C" { |
| 22 | + /// fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void; |
| 23 | + /// } |
| 24 | + /// let ptr = unsafe { memcpy(dest, src, size); } |
| 25 | + /// // Or use via libc |
| 26 | + /// let ptr = unsafe { libc::memcpy(dest, src, size); } |
| 27 | + #[clippy::version = "1.70.0"] |
| 28 | + pub MEM_UNSAFE_FUNCTIONS, |
| 29 | + nursery, |
| 30 | + "use of potentially dangerous external functions" |
| 31 | +} |
| 32 | + |
| 33 | +declare_lint_pass!(GuidelineLints => [ |
| 34 | + MEM_UNSAFE_FUNCTIONS, |
| 35 | +]); |
| 36 | + |
| 37 | +impl<'tcx> LateLintPass<'tcx> for GuidelineLints { |
| 38 | + fn check_fn( |
| 39 | + &mut self, |
| 40 | + cx: &LateContext<'tcx>, |
| 41 | + _kind: intravisit::FnKind<'tcx>, |
| 42 | + _decl: &'tcx hir::FnDecl<'_>, |
| 43 | + _body: &'tcx hir::Body<'_>, |
| 44 | + span: Span, |
| 45 | + _def_id: LocalDefId, |
| 46 | + ) { |
| 47 | + mem_unsafe_functions::check(cx, span); |
| 48 | + } |
| 49 | + |
| 50 | + fn check_item(&mut self, _cx: &LateContext<'tcx>, _item: &'tcx hir::Item<'_>) {} |
| 51 | +} |
0 commit comments