-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #9105 - Serial-ATA:lint-invalid-utf8, r=Jarcho
Add `invalid_utf8_in_unchecked` changelog: Add [`invalid_utf8_in_unchecked`] closes: #629 Don't know how useful of a lint this is, just saw this was a really old issue 😄.
- Loading branch information
Showing
9 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::{match_function_call, paths}; | ||
use rustc_ast::{BorrowKind, LitKind}; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::source_map::Spanned; | ||
use rustc_span::Span; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for `std::str::from_utf8_unchecked` with an invalid UTF-8 literal | ||
/// | ||
/// ### Why is this bad? | ||
/// Creating such a `str` would result in undefined behavior | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// # #[allow(unused)] | ||
/// unsafe { | ||
/// std::str::from_utf8_unchecked(b"cl\x82ippy"); | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.64.0"] | ||
pub INVALID_UTF8_IN_UNCHECKED, | ||
correctness, | ||
"using a non UTF-8 literal in `std::std::from_utf8_unchecked`" | ||
} | ||
declare_lint_pass!(InvalidUtf8InUnchecked => [INVALID_UTF8_IN_UNCHECKED]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for InvalidUtf8InUnchecked { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if let Some([arg]) = match_function_call(cx, expr, &paths::STR_FROM_UTF8_UNCHECKED) { | ||
match &arg.kind { | ||
ExprKind::Lit(Spanned { node: lit, .. }) => { | ||
if let LitKind::ByteStr(bytes) = &lit | ||
&& std::str::from_utf8(bytes).is_err() | ||
{ | ||
lint(cx, expr.span); | ||
} | ||
}, | ||
ExprKind::AddrOf(BorrowKind::Ref, _, Expr { kind: ExprKind::Array(args), .. }) => { | ||
let elements = args.iter().map(|e|{ | ||
match &e.kind { | ||
ExprKind::Lit(Spanned { node: lit, .. }) => match lit { | ||
LitKind::Byte(b) => Some(*b), | ||
#[allow(clippy::cast_possible_truncation)] | ||
LitKind::Int(b, _) => Some(*b as u8), | ||
_ => None | ||
} | ||
_ => None | ||
} | ||
}).collect::<Option<Vec<_>>>(); | ||
|
||
if let Some(elements) = elements | ||
&& std::str::from_utf8(&elements).is_err() | ||
{ | ||
lint(cx, expr.span); | ||
} | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn lint(cx: &LateContext<'_>, span: Span) { | ||
span_lint( | ||
cx, | ||
INVALID_UTF8_IN_UNCHECKED, | ||
span, | ||
"non UTF-8 literal in `std::str::from_utf8_unchecked`", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#![warn(clippy::invalid_utf8_in_unchecked)] | ||
|
||
fn main() { | ||
// Valid | ||
unsafe { | ||
std::str::from_utf8_unchecked(&[99, 108, 105, 112, 112, 121]); | ||
std::str::from_utf8_unchecked(&[b'c', b'l', b'i', b'p', b'p', b'y']); | ||
std::str::from_utf8_unchecked(b"clippy"); | ||
|
||
let x = 0xA0; | ||
std::str::from_utf8_unchecked(&[0xC0, x]); | ||
} | ||
|
||
// Invalid | ||
unsafe { | ||
std::str::from_utf8_unchecked(&[99, 108, 130, 105, 112, 112, 121]); | ||
std::str::from_utf8_unchecked(&[b'c', b'l', b'\x82', b'i', b'p', b'p', b'y']); | ||
std::str::from_utf8_unchecked(b"cl\x82ippy"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: non UTF-8 literal in `std::str::from_utf8_unchecked` | ||
--> $DIR/invalid_utf8_in_unchecked.rs:16:9 | ||
| | ||
LL | std::str::from_utf8_unchecked(&[99, 108, 130, 105, 112, 112, 121]); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::invalid-utf8-in-unchecked` implied by `-D warnings` | ||
|
||
error: non UTF-8 literal in `std::str::from_utf8_unchecked` | ||
--> $DIR/invalid_utf8_in_unchecked.rs:17:9 | ||
| | ||
LL | std::str::from_utf8_unchecked(&[b'c', b'l', b'/x82', b'i', b'p', b'p', b'y']); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: non UTF-8 literal in `std::str::from_utf8_unchecked` | ||
--> $DIR/invalid_utf8_in_unchecked.rs:18:9 | ||
| | ||
LL | std::str::from_utf8_unchecked(b"cl/x82ippy"); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|