Skip to content

Commit

Permalink
Fix temporary_cstring_as_ptr false negative
Browse files Browse the repository at this point in the history
Fixes #4375.

Changes the check to test when `.unwrap().as_ptr()` is called on any
`Result<CString, _>` as suggested by @flip1995
(#4375 (comment)).
  • Loading branch information
Michael Wright committed Aug 21, 2019
1 parent 460e265 commit 59893bc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
12 changes: 5 additions & 7 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::iter;
use if_chain::if_chain;
use matches::matches;
use rustc::hir;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::intravisit::{self, Visitor};
use rustc::lint::{in_external_macro, LateContext, LateLintPass, Lint, LintArray, LintContext, LintPass};
use rustc::ty::{self, Predicate, Ty};
Expand Down Expand Up @@ -1668,13 +1667,12 @@ fn lint_extend(cx: &LateContext<'_, '_>, expr: &hir::Expr, args: &[hir::Expr]) {
}
}

fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, new: &hir::Expr, unwrap: &hir::Expr) {
fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, source: &hir::Expr, unwrap: &hir::Expr) {
if_chain! {
if let hir::ExprKind::Call(ref fun, ref args) = new.node;
if args.len() == 1;
if let hir::ExprKind::Path(ref path) = fun.node;
if let Res::Def(DefKind::Method, did) = cx.tables.qpath_res(path, fun.hir_id);
if match_def_path(cx, did, &paths::CSTRING_NEW);
let source_type = cx.tables.expr_ty(source);
if let ty::Adt(def, substs) = source_type.sty;
if match_def_path(cx, def.did, &paths::RESULT);
if match_type(cx, substs.type_at(0), &paths::CSTRING);
then {
span_lint_and_then(
cx,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
pub const CMP_MAX: [&str; 3] = ["core", "cmp", "max"];
pub const CMP_MIN: [&str; 3] = ["core", "cmp", "min"];
pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"];
pub const CSTRING_NEW: [&str; 5] = ["std", "ffi", "c_str", "CString", "new"];
pub const CSTRING: [&str; 4] = ["std", "ffi", "c_str", "CString"];
pub const DEFAULT_TRAIT: [&str; 3] = ["core", "default", "Default"];
pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "default"];
pub const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];
Expand Down
17 changes: 16 additions & 1 deletion tests/ui/cstring.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
#![deny(clippy::temporary_cstring_as_ptr)]

fn main() {}

#[allow(clippy::result_unwrap_used)]
fn temporary_cstring() {
use std::ffi::CString;

CString::new("foo").unwrap().as_ptr();
CString::new("foo").expect("dummy").as_ptr();
}

mod issue4375 {
use std::ffi::CString;
use std::os::raw::c_char;

extern "C" {
fn foo(data: *const c_char);
}

pub fn bar(v: &[u8]) {
let cstr = CString::new(v);
unsafe { foo(cstr.unwrap().as_ptr()) }
}
}
29 changes: 23 additions & 6 deletions tests/ui/cstring.stderr
Original file line number Diff line number Diff line change
@@ -1,29 +1,46 @@
error: you are getting the inner pointer of a temporary `CString`
--> $DIR/cstring.rs:7:5
--> $DIR/cstring.rs:8:5
|
LL | CString::new("foo").unwrap().as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[deny(clippy::temporary_cstring_as_ptr)]` on by default
note: lint level defined here
--> $DIR/cstring.rs:1:9
|
LL | #![deny(clippy::temporary_cstring_as_ptr)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: that pointer will be invalid outside this expression
help: assign the `CString` to a variable to extend its lifetime
--> $DIR/cstring.rs:7:5
--> $DIR/cstring.rs:8:5
|
LL | CString::new("foo").unwrap().as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: you are getting the inner pointer of a temporary `CString`
--> $DIR/cstring.rs:8:5
--> $DIR/cstring.rs:9:5
|
LL | CString::new("foo").expect("dummy").as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: that pointer will be invalid outside this expression
help: assign the `CString` to a variable to extend its lifetime
--> $DIR/cstring.rs:8:5
--> $DIR/cstring.rs:9:5
|
LL | CString::new("foo").expect("dummy").as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors
error: you are getting the inner pointer of a temporary `CString`
--> $DIR/cstring.rs:22:22
|
LL | unsafe { foo(cstr.unwrap().as_ptr()) }
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: that pointer will be invalid outside this expression
help: assign the `CString` to a variable to extend its lifetime
--> $DIR/cstring.rs:22:22
|
LL | unsafe { foo(cstr.unwrap().as_ptr()) }
| ^^^^^^^^^^^^^

error: aborting due to 3 previous errors

0 comments on commit 59893bc

Please sign in to comment.