Skip to content

Commit 8d5d9e0

Browse files
committed
Auto merge of #8250 - pr2502:fix_repeat_underflow, r=giraffate
Fix underflow in `manual_split_once` lint Hi, a friend found clippy started crashing on a suspiciously large allocation of `u64::MAX` memory on their code. The mostly minimized repro is: ```rust fn _f01(title: &str) -> Option<()> { let _ = title[1..].splitn(2, '[').next()?; Some(()) } ``` The underflow happens in this case on line 57 of the patch but I've changed the other substraction to saturating as well since it could potentially cause the same issue. I'm not sure where to put a regression test, or if it's even worth for such a thing. Aside, has it been considered before to build clippy with overflow checks enabled? changelog: fix ICE of underflow in `manual_split_once` lint
2 parents fb94992 + 23fd95a commit 8d5d9e0

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

clippy_lints/src/methods/str_splitn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ pub(super) fn check_manual_split_once(
4545
IterUsageKind::Next | IterUsageKind::Second => {
4646
let self_deref = {
4747
let adjust = cx.typeck_results().expr_adjustments(self_arg);
48-
if adjust.is_empty() {
48+
if adjust.len() < 2 {
4949
String::new()
5050
} else if cx.typeck_results().expr_ty(self_arg).is_box()
5151
|| adjust
5252
.iter()
5353
.any(|a| matches!(a.kind, Adjust::Deref(Some(_))) || a.target.is_box())
5454
{
55-
format!("&{}", "*".repeat(adjust.len() - 1))
55+
format!("&{}", "*".repeat(adjust.len().saturating_sub(1)))
5656
} else {
57-
"*".repeat(adjust.len() - 2)
57+
"*".repeat(adjust.len().saturating_sub(2))
5858
}
5959
};
6060
if matches!(usage.kind, IterUsageKind::Next) {

tests/ui/crashes/ice-8250.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn _f(s: &str) -> Option<()> {
2+
let _ = s[1..].splitn(2, '.').next()?;
3+
Some(())
4+
}
5+
6+
fn main() {}

tests/ui/crashes/ice-8250.stderr

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: manual implementation of `split_once`
2+
--> $DIR/ice-8250.rs:2:13
3+
|
4+
LL | let _ = s[1..].splitn(2, '.').next()?;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `s[1..].split_once('.').map_or(s[1..], |x| x.0)`
6+
|
7+
= note: `-D clippy::manual-split-once` implied by `-D warnings`
8+
9+
error: unnecessary use of `splitn`
10+
--> $DIR/ice-8250.rs:2:13
11+
|
12+
LL | let _ = s[1..].splitn(2, '.').next()?;
13+
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `s[1..].split('.')`
14+
|
15+
= note: `-D clippy::needless-splitn` implied by `-D warnings`
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)