Skip to content

Commit da3cc15

Browse files
committed
add guidelines type and barebone for [mem_unsafe_functions] lint
Signed-off-by: J-ZhengLi <lizheng135@huawei.com>
1 parent 83e42a2 commit da3cc15

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4707,6 +4707,7 @@ Released 2018-09-13
47074707
[`mem_replace_option_with_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
47084708
[`mem_replace_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default
47094709
[`mem_replace_with_uninit`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit
4710+
[`mem_unsafe_functions`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_unsafe_functions
47104711
[`min_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_max
47114712
[`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute
47124713
[`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os

clippy_lints/src/declared_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
191191
crate::functions::TOO_MANY_ARGUMENTS_INFO,
192192
crate::functions::TOO_MANY_LINES_INFO,
193193
crate::future_not_send::FUTURE_NOT_SEND_INFO,
194+
crate::guidelines::MEM_UNSAFE_FUNCTIONS_INFO,
194195
crate::if_let_mutex::IF_LET_MUTEX_INFO,
195196
crate::if_not_else::IF_NOT_ELSE_INFO,
196197
crate::if_then_some_else_none::IF_THEN_SOME_ELSE_NONE_INFO,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use rustc_lint::LateContext;
3+
use rustc_span::Span;
4+
5+
use super::MEM_UNSAFE_FUNCTIONS;
6+
7+
// TODO: Adjust the parameters as necessary
8+
pub(super) fn check(cx: &LateContext<'_>, span: Span) {
9+
span_lint(cx, MEM_UNSAFE_FUNCTIONS, span, "it's working!");
10+
}

clippy_lints/src/guidelines/mod.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

clippy_lints/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ mod from_raw_with_void_ptr;
138138
mod from_str_radix_10;
139139
mod functions;
140140
mod future_not_send;
141+
mod guidelines;
141142
mod if_let_mutex;
142143
mod if_not_else;
143144
mod if_then_some_else_none;
@@ -959,6 +960,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
959960
store.register_late_pass(|_| Box::new(tests_outside_test_module::TestsOutsideTestModule));
960961
store.register_late_pass(|_| Box::new(manual_slice_size_calculation::ManualSliceSizeCalculation));
961962
store.register_early_pass(|| Box::new(suspicious_doc_comments::SuspiciousDocComments));
963+
store.register_late_pass(|_| Box::new(guidelines::GuidelineLints));
962964
// add lints here, do not remove this comment, it's used in `new_lint`
963965
}
964966

tests/ui/mem_unsafe_functions.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(c_size_t)]
2+
#![warn(clippy::mem_unsafe_functions)]
3+
4+
use core::ffi::{c_char, c_int, c_size_t, c_void};
5+
6+
// mock libc crate
7+
mod libc {
8+
pub use core::ffi::c_size_t as size_t;
9+
pub use core::ffi::{c_char, c_int, c_void};
10+
extern "C" {
11+
pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
12+
pub fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t) -> *mut c_char;
13+
pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
14+
pub fn memmove(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
15+
pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
16+
}
17+
}
18+
19+
extern "C" {
20+
fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
21+
fn strncpy(dst: *mut c_char, src: *const c_char, n: c_size_t) -> *mut c_char;
22+
fn memcpy(dest: *mut c_void, src: *const c_void, n: c_size_t) -> *mut c_void;
23+
fn memmove(dest: *mut c_void, src: *const c_void, n: c_size_t) -> *mut c_void;
24+
fn memset(dest: *mut c_void, c: c_int, n: c_size_t) -> *mut c_void;
25+
}
26+
27+
fn main() {}

tests/ui/mem_unsafe_functions.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: it's working!
2+
--> $DIR/mem_unsafe_functions.rs:27:1
3+
|
4+
LL | fn main() {}
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::mem-unsafe-functions` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)