Skip to content

Commit

Permalink
Auto merge of #5129 - JohnTitor:use-checked-sub, r=flip1995
Browse files Browse the repository at this point in the history
Use `checked_sub` to avoid index out of bounds

(Fixes) #4681 (possibly)

The issue likely occurs due to `lit_snip.len() < suffix.len() + 1`. You can see similar backtrace to change it to `lit_snip.len() - suffix.len() - 1000` or something then run `cargo test --release`.
But I couldn't come up with the test so I'd leave the issue open if we want.

changelog: Fix potential ICE in `misc_early`
  • Loading branch information
bors committed Feb 3, 2020
2 parents fdc6690 + bae129a commit a395894
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,11 @@ impl MiscEarlyLints {
LitIntType::Unsuffixed => "",
};

let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1;
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
val
} else {
return; // It's useless so shouldn't lint.
};
// Do not lint when literal is unsuffixed.
if !suffix.is_empty() && lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
span_lint_and_sugg(
Expand All @@ -502,7 +506,7 @@ impl MiscEarlyLints {
);
}

if lit_snip.starts_with("0x") {
if lit_snip.starts_with("0x") && maybe_last_sep_idx >= 3 {
let mut seen = (false, false);
for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
match ch {
Expand Down Expand Up @@ -546,7 +550,11 @@ impl MiscEarlyLints {
}
} else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
let suffix = float_ty.name_str();
let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1;
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
val
} else {
return; // It's useless so shouldn't lint.
};
if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
span_lint_and_sugg(
cx,
Expand Down

0 comments on commit a395894

Please sign in to comment.