Skip to content

Commit 64d74df

Browse files
committed
Auto merge of #7243 - mgacek8:issue7145_strlen_on_c_strings, r=giraffate
Add new lint: `strlen_on_c_strings` ~~This is WIP, linting in case of `CString` has been added, but for `CStr`, its diagnostic item needs to be available for clippy. [PR that adds diagnostic item for CStr on rust repo](#85439 Ready for the review. Please take a look. fixes #7145 changelog: Add new lint: `strlen_on_c_strings`, that lints on `libc::strlen(some_cstring.as_ptr())`
2 parents 3cc6778 + 59a164e commit 64d74df

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2798,6 +2798,7 @@ Released 2018-09-13
27982798
[`string_from_utf8_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_from_utf8_as_bytes
27992799
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
28002800
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
2801+
[`strlen_on_c_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#strlen_on_c_strings
28012802
[`struct_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools
28022803
[`suboptimal_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
28032804
[`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
@@ -338,6 +338,7 @@ mod size_of_in_element_count;
338338
mod slow_vector_initialization;
339339
mod stable_sort_primitive;
340340
mod strings;
341+
mod strlen_on_c_strings;
341342
mod suspicious_operation_groupings;
342343
mod suspicious_trait_impl;
343344
mod swap;
@@ -914,6 +915,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
914915
strings::STRING_LIT_AS_BYTES,
915916
strings::STRING_TO_STRING,
916917
strings::STR_TO_STRING,
918+
strlen_on_c_strings::STRLEN_ON_C_STRINGS,
917919
suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS,
918920
suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL,
919921
suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL,
@@ -1410,6 +1412,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
14101412
LintId::of(slow_vector_initialization::SLOW_VECTOR_INITIALIZATION),
14111413
LintId::of(stable_sort_primitive::STABLE_SORT_PRIMITIVE),
14121414
LintId::of(strings::STRING_FROM_UTF8_AS_BYTES),
1415+
LintId::of(strlen_on_c_strings::STRLEN_ON_C_STRINGS),
14131416
LintId::of(suspicious_trait_impl::SUSPICIOUS_ARITHMETIC_IMPL),
14141417
LintId::of(suspicious_trait_impl::SUSPICIOUS_OP_ASSIGN_IMPL),
14151418
LintId::of(swap::ALMOST_SWAPPED),
@@ -1639,6 +1642,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16391642
LintId::of(reference::REF_IN_DEREF),
16401643
LintId::of(repeat_once::REPEAT_ONCE),
16411644
LintId::of(strings::STRING_FROM_UTF8_AS_BYTES),
1645+
LintId::of(strlen_on_c_strings::STRLEN_ON_C_STRINGS),
16421646
LintId::of(swap::MANUAL_SWAP),
16431647
LintId::of(temporary_assignment::TEMPORARY_ASSIGNMENT),
16441648
LintId::of(transmute::CROSSPOINTER_TRANSMUTE),
@@ -2096,6 +2100,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
20962100
store.register_late_pass(move || box missing_enforced_import_rename::ImportRename::new(import_renames.clone()));
20972101
let scripts = conf.allowed_scripts.clone();
20982102
store.register_early_pass(move || box disallowed_script_idents::DisallowedScriptIdents::new(&scripts));
2103+
store.register_late_pass(|| box strlen_on_c_strings::StrlenOnCStrings);
20992104
}
21002105

21012106
#[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 (you might also need to get rid of `unsafe` block in some cases):",
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::strlen-on-c-strings` implied by `-D warnings`
8+
help: try this (you might also need to get rid of `unsafe` block in some cases):
9+
|
10+
LL | let len = unsafe { cstring.as_bytes().len() };
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: using `libc::strlen` on a `CString` or `CStr` value
14+
--> $DIR/strlen_on_c_strings.rs:15:24
15+
|
16+
LL | let len = unsafe { libc::strlen(cstr.as_ptr()) };
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
help: try this (you might also need to get rid of `unsafe` block in some cases):
20+
|
21+
LL | let len = unsafe { cstr.to_bytes().len() };
22+
| ^^^^^^^^^^^^^^^^^^^^^
23+
24+
error: aborting due to 2 previous errors
25+

0 commit comments

Comments
 (0)