Skip to content

Commit a395894

Browse files
committed
Auto merge of #5129 - JohnTitor:use-checked-sub, r=flip1995
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`
2 parents fdc6690 + bae129a commit a395894

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

clippy_lints/src/misc_early.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,11 @@ impl MiscEarlyLints {
488488
LitIntType::Unsuffixed => "",
489489
};
490490

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

505-
if lit_snip.starts_with("0x") {
509+
if lit_snip.starts_with("0x") && maybe_last_sep_idx >= 3 {
506510
let mut seen = (false, false);
507511
for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
508512
match ch {
@@ -546,7 +550,11 @@ impl MiscEarlyLints {
546550
}
547551
} else if let LitKind::Float(_, LitFloatType::Suffixed(float_ty)) = lit.kind {
548552
let suffix = float_ty.name_str();
549-
let maybe_last_sep_idx = lit_snip.len() - suffix.len() - 1;
553+
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
554+
val
555+
} else {
556+
return; // It's useless so shouldn't lint.
557+
};
550558
if lit_snip.as_bytes()[maybe_last_sep_idx] != b'_' {
551559
span_lint_and_sugg(
552560
cx,

0 commit comments

Comments
 (0)