Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subtraction overflow in cast_possible_truncation #8687

Merged
merged 1 commit into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions clippy_lints/src/casts/cast_possible_truncation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ fn apply_reductions(cx: &LateContext<'_>, nbits: u64, expr: &Expr<'_>, signed: b
ExprKind::Block(block, _) => block.expr.map_or(nbits, |e| apply_reductions(cx, nbits, e, signed)),
ExprKind::Binary(op, left, right) => match op.node {
BinOpKind::Div => {
apply_reductions(cx, nbits, left, signed)
- (if signed {
0 // let's be conservative here
} else {
// by dividing by 1, we remove 0 bits, etc.
get_constant_bits(cx, right).map_or(0, |b| b.saturating_sub(1))
})
apply_reductions(cx, nbits, left, signed).saturating_sub(if signed {
// let's be conservative here
0
} else {
// by dividing by 1, we remove 0 bits, etc.
get_constant_bits(cx, right).map_or(0, |b| b.saturating_sub(1))
})
},
BinOpKind::Rem | BinOpKind::BitAnd => get_constant_bits(cx, right)
.unwrap_or(u64::max_value())
.min(apply_reductions(cx, nbits, left, signed)),
BinOpKind::Shr => {
apply_reductions(cx, nbits, left, signed)
- constant_int(cx, right).map_or(0, |s| u64::try_from(s).expect("shift too high"))
},
BinOpKind::Shr => apply_reductions(cx, nbits, left, signed)
.saturating_sub(constant_int(cx, right).map_or(0, |s| u64::try_from(s).expect("shift too high"))),
_ => nbits,
},
ExprKind::MethodCall(method, [left, right], _) => {
Expand Down
14 changes: 11 additions & 3 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#![feature(repr128)]
#![allow(incomplete_features)]

#[warn(
#![warn(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
clippy::cast_possible_wrap
)]
#[allow(clippy::cast_abs_to_unsigned, clippy::no_effect, clippy::unnecessary_operation)]
#![allow(clippy::cast_abs_to_unsigned, clippy::no_effect, clippy::unnecessary_operation)]

fn main() {
// Test clippy::cast_precision_loss
let x0 = 1i32;
Expand Down Expand Up @@ -252,3 +252,11 @@ fn main() {
}
}
}

fn avoid_subtract_overflow(q: u32) {
let c = (q >> 16) as u8;
c as usize;

let c = (q / 1000) as u8;
c as usize;
}
14 changes: 13 additions & 1 deletion tests/ui/cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -194,5 +194,17 @@ error: casting `main::E10` to `u16` may truncate the value
LL | let _ = self as u16;
| ^^^^^^^^^^^

error: aborting due to 31 previous errors
error: casting `u32` to `u8` may truncate the value
--> $DIR/cast.rs:257:13
|
LL | let c = (q >> 16) as u8;
| ^^^^^^^^^^^^^^^

error: casting `u32` to `u8` may truncate the value
--> $DIR/cast.rs:260:13
|
LL | let c = (q / 1000) as u8;
| ^^^^^^^^^^^^^^^^

error: aborting due to 33 previous errors