Skip to content

Commit 9ef47c9

Browse files
committed
Add new lint: strlen_on_c_strings
1 parent f1f5ccd commit 9ef47c9

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,7 @@ Released 2018-09-13
26652665
[`string_from_utf8_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_from_utf8_as_bytes
26662666
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
26672667
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
2668+
[`strlen_on_c_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#strlen_on_c_strings
26682669
[`struct_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools
26692670
[`suboptimal_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
26702671
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl

clippy_lints/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ mod size_of_in_element_count;
330330
mod slow_vector_initialization;
331331
mod stable_sort_primitive;
332332
mod strings;
333+
mod strlen_on_c_strings;
333334
mod suspicious_operation_groupings;
334335
mod suspicious_trait_impl;
335336
mod swap;
@@ -902,6 +903,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
902903
strings::STRING_LIT_AS_BYTES,
903904
strings::STRING_TO_STRING,
904905
strings::STR_TO_STRING,
906+
strlen_on_c_strings::STRLEN_ON_C_STRINGS,
905907
suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS,
906908
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
907909
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
@@ -1393,6 +1395,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13931395
LintId::of(slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
13941396
LintId::of(stable_sort_primitive::STABLE_SORT_PRIMITIVE),
13951397
LintId::of(strings::STRING_FROM_UTF8_AS_BYTES),
1398+
LintId::of(strlen_on_c_strings::STRLEN_ON_C_STRINGS),
13961399
LintId::of(suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
13971400
LintId::of(suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL),
13981401
LintId::of(swap::ALMOST_SWAPPED),
@@ -1632,6 +1635,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16321635
LintId::of(reference::REF_IN_DEREF),
16331636
LintId::of(repeat_once::REPEAT_ONCE),
16341637
LintId::of(strings::STRING_FROM_UTF8_AS_BYTES),
1638+
LintId::of(strlen_on_c_strings::STRLEN_ON_C_STRINGS),
16351639
LintId::of(swap::MANUAL_SWAP),
16361640
LintId::of(temporary_assignment::TEMPORARY_ASSIGNMENT),
16371641
LintId::of(transmute::CROSSPOINTER_TRANSMUTE),
@@ -2070,6 +2074,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
20702074
let disallowed_types = conf.disallowed_types.iter().cloned().collect::<FxHashSet<_>>();
20712075
store.register_late_pass(move || box disallowed_type::DisallowedType::new(&disallowed_types));
20722076

2077+
store.register_late_pass(|| box strlen_on_c_strings::StrlenOnCStrings);
20732078
}
20742079

20752080
#[rustfmt::skip]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::in_macro;
3+
use clippy_utils::paths;
4+
use clippy_utils::source::snippet_with_macro_callsite;
5+
use clippy_utils::ty::{is_type_diagnostic_item, is_type_ref_to_diagnostic_item};
6+
use if_chain::if_chain;
7+
use rustc_errors::Applicability;
8+
use rustc_hir as hir;
9+
use rustc_lint::{LateContext, LateLintPass};
10+
use rustc_session::{declare_lint_pass, declare_tool_lint};
11+
use rustc_span::symbol::{sym, Symbol};
12+
13+
declare_clippy_lint! {
14+
/// **What it does:** Checks for usage of `libc::strlen` on a `CString` or `CStr` value,
15+
/// and suggest calling `as_bytes().len()` or `to_bytes().len()` respectively instead.
16+
///
17+
/// **Why is this bad?** This avoids calling an unsafe `libc` function.
18+
/// Currently, it also avoids calculating the length.
19+
///
20+
/// **Known problems:** None.
21+
///
22+
/// **Example:**
23+
///
24+
/// ```rust, ignore
25+
/// use std::ffi::CString;
26+
/// let cstring = CString::new("foo").expect("CString::new failed");
27+
/// let len = unsafe { libc::strlen(cstring.as_ptr()) };
28+
/// ```
29+
/// Use instead:
30+
/// ```rust, no_run
31+
/// use std::ffi::CString;
32+
/// let cstring = CString::new("foo").expect("CString::new failed");
33+
/// let len = cstring.as_bytes().len();
34+
/// ```
35+
pub STRLEN_ON_C_STRINGS,
36+
complexity,
37+
"using `libc::strlen` on a `CString` or `CStr` value, while `as_bytes().len()` or `to_bytes().len()` respectively can be used instead"
38+
}
39+
40+
declare_lint_pass!(StrlenOnCStrings => [STRLEN_ON_C_STRINGS]);
41+
42+
impl LateLintPass<'tcx> for StrlenOnCStrings {
43+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
44+
if in_macro(expr.span) {
45+
return;
46+
}
47+
48+
if_chain! {
49+
if let hir::ExprKind::Call(func, [recv]) = expr.kind;
50+
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = func.kind;
51+
52+
if (&paths::LIBC_STRLEN).iter().map(|x| Symbol::intern(x)).eq(
53+
path.segments.iter().map(|seg| seg.ident.name));
54+
if let hir::ExprKind::MethodCall(path, _, args, _) = recv.kind;
55+
if args.len() == 1;
56+
if !args.iter().any(|e| e.span.from_expansion());
57+
if path.ident.name == sym::as_ptr;
58+
then {
59+
let cstring = &args[0];
60+
let ty = cx.typeck_results().expr_ty(cstring);
61+
let val_name = snippet_with_macro_callsite(cx, cstring.span, "..");
62+
let sugg = if is_type_diagnostic_item(cx, ty, sym::cstring_type){
63+
format!("{}.as_bytes().len()", val_name)
64+
} else if is_type_ref_to_diagnostic_item(cx, ty, sym::CStr){
65+
format!("{}.to_bytes().len()", val_name)
66+
} else {
67+
return;
68+
};
69+
70+
span_lint_and_sugg(
71+
cx,
72+
STRLEN_ON_C_STRINGS,
73+
expr.span,
74+
"using `libc::strlen` on a `CString` or `CStr` value",
75+
"try this",
76+
sugg,
77+
Applicability::Unspecified // Sometimes unnecessary `unsafe` block
78+
);
79+
}
80+
}
81+
}
82+
}

clippy_utils/src/paths.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ pub const ITER_REPEAT: [&str; 5] = ["core", "iter", "sources", "repeat", "repeat
8282
pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
8383
#[cfg(feature = "internal-lints")]
8484
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
85+
pub const LIBC_STRLEN: [&str; 2] = ["libc", "strlen"];
8586
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
8687
#[cfg(any(feature = "internal-lints", feature = "metadata-collector-lint"))]
8788
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];

clippy_utils/src/ty.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,17 @@ pub fn is_recursively_primitive_type(ty: Ty<'_>) -> bool {
231231
}
232232
}
233233

234+
/// Checks if the type is a reference equals to a diagnostic item
235+
pub fn is_type_ref_to_diagnostic_item(cx: &LateContext<'_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
236+
match ty.kind() {
237+
ty::Ref(_, ref_ty, _) => match ref_ty.kind() {
238+
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
239+
_ => false,
240+
},
241+
_ => false,
242+
}
243+
}
244+
234245
/// Checks if the type is equal to a diagnostic item
235246
///
236247
/// If you change the signature, remember to update the internal lint `MatchTypeOnDiagItem`

tests/ui/strlen_on_c_strings.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![warn(clippy::strlen_on_c_strings)]
2+
#![allow(dead_code)]
3+
#![feature(rustc_private)]
4+
extern crate libc;
5+
6+
use std::ffi::{CStr, CString};
7+
8+
fn main() {
9+
// CString
10+
let cstring = CString::new("foo").expect("CString::new failed");
11+
let len = unsafe { libc::strlen(cstring.as_ptr()) };
12+
13+
// CStr
14+
let cstr = CStr::from_bytes_with_nul(b"foo\0").expect("CStr::from_bytes_with_nul failed");
15+
let len = unsafe { libc::strlen(cstr.as_ptr()) };
16+
}

tests/ui/strlen_on_c_strings.stderr

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: using `libc::strlen` on a `CString` or `CStr` value
2+
--> $DIR/strlen_on_c_strings.rs:11:24
3+
|
4+
LL | let len = unsafe { libc::strlen(cstring.as_ptr()) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstring.as_bytes().len()`
6+
|
7+
= note: `-D clippy::strlen-on-c-strings` implied by `-D warnings`
8+
9+
error: using `libc::strlen` on a `CString` or `CStr` value
10+
--> $DIR/strlen_on_c_strings.rs:15:24
11+
|
12+
LL | let len = unsafe { libc::strlen(cstr.as_ptr()) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `cstr.to_bytes().len()`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)