Skip to content

Commit

Permalink
Lint casting integers to dangling pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasstevens committed Dec 19, 2017
1 parent cf58e1c commit ec69a57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
38 changes: 32 additions & 6 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ declare_lint! {
"using 0 as *{const, mut} T"
}

/// **What it does:** Catch casts from `1` to some pointer type
///
/// **Why is this bad?** This generally means a dangling pointer and is better expressed as
/// {`std`, `core`}`::ptr::`{`dangling`, `dangling_mut`}.
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// 1 as *const u32
/// ```
declare_lint! {
pub DANGLING_PTR,
Allow,
"using 1 as *{const, mut} T"
}

/// **What it does:** Checks for (in-)equality comparisons on floating-point
/// value and constant, except in functions called `*eq*` (which probably
/// implement equality for a type involving floats).
Expand Down Expand Up @@ -237,6 +255,7 @@ impl LintPass for Pass {
USED_UNDERSCORE_BINDING,
SHORT_CIRCUIT_STATEMENT,
ZERO_PTR,
DANGLING_PTR,
FLOAT_CMP_CONST
)
}
Expand Down Expand Up @@ -628,14 +647,21 @@ fn check_cast(cx: &LateContext, span: Span, e: &Expr, ty: &Ty) {
if let TyPtr(MutTy { mutbl, .. }) = ty.node;
if let ExprLit(ref lit) = e.node;
if let LitKind::Int(value, ..) = lit.node;
if value == 0;
if !in_constant(cx, e.id);
then {
let msg = match mutbl {
Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`",
Mutability::MutImmutable => "`0 as *const _` detected. Consider using `ptr::null()`",
};
span_lint(cx, ZERO_PTR, span, msg);
if value == 0 {
let msg = match mutbl {
Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`",
Mutability::MutImmutable => "`0 as *const _` detected. Consider using `ptr::null()`",
};
span_lint(cx, ZERO_PTR, span, msg);
} else if value == 1 {
let msg = match mutbl {
Mutability::MutMutable => "`1 as *mut _` detected. Consider using `ptr::dangling_mut()`",
Mutability::MutImmutable => "`1 as *const _` detected. Consider using `ptr::dangling()`",
};
span_lint(cx, DANGLING_PTR, span, msg);
}
}
}
}
10 changes: 10 additions & 0 deletions tests/ui/dangling_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![warn(dangling_ptr)]

#[allow(unused_variables)]
fn main() {
let x = 1 as *const usize;
let y = 1 as *mut f64;

let z = 1;
let z = z as *const usize; // this is currently not caught
}
14 changes: 14 additions & 0 deletions tests/ui/dangling_ptr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: `1 as *const _` detected. Consider using `ptr::dangling()`
--> $DIR/dangling_ptr.rs:5:13
|
5 | let x = 1 as *const usize;
| ^^^^^^^^^^^^^^^^^
|
= note: `-D dangling-ptr` implied by `-D warnings`

error: `1 as *mut _` detected. Consider using `ptr::dangling_mut()`
--> $DIR/dangling_ptr.rs:6:13
|
6 | let y = 1 as *mut f64;
| ^^^^^^^^^^^^^

0 comments on commit ec69a57

Please sign in to comment.