Skip to content

Commit

Permalink
Auto merge of rust-lang#10103 - Niki4tap:overflow_ice, r=flip1995
Browse files Browse the repository at this point in the history
Fix overflow ICE in large_stack/const_arrays

Change `maximum_allowed_size` config variable type from `u64` to `u128`, and converting total array sizes to `u128` to avoid overflow during multiplication.

Fixes rust-lang#10044

changelog: Fix: [`large_const_arrays`] and [`large_stack_arrays`]: avoid integer overflow when calculating total array size
rust-lang#10103
  • Loading branch information
bors committed Dec 19, 2022
2 parents b3145fe + d0ac6ba commit 09cfcaf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
6 changes: 3 additions & 3 deletions clippy_lints/src/large_const_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ declare_clippy_lint! {
}

pub struct LargeConstArrays {
maximum_allowed_size: u64,
maximum_allowed_size: u128,
}

impl LargeConstArrays {
#[must_use]
pub fn new(maximum_allowed_size: u64) -> Self {
pub fn new(maximum_allowed_size: u128) -> Self {
Self { maximum_allowed_size }
}
}
Expand All @@ -56,7 +56,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
if let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx);
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
if self.maximum_allowed_size < element_count * element_size;
if self.maximum_allowed_size < u128::from(element_count) * u128::from(element_size);

then {
let hi_pos = item.ident.span.lo() - BytePos::from_usize(1);
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/large_stack_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ declare_clippy_lint! {
}

pub struct LargeStackArrays {
maximum_allowed_size: u64,
maximum_allowed_size: u128,
}

impl LargeStackArrays {
#[must_use]
pub fn new(maximum_allowed_size: u64) -> Self {
pub fn new(maximum_allowed_size: u128) -> Self {
Self { maximum_allowed_size }
}
}
Expand All @@ -45,7 +45,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
&& !cx.tcx.hir().parent_iter(expr.hir_id)
.any(|(_, node)| matches!(node, Node::Item(Item { kind: ItemKind::Static(..), .. })))
&& self.maximum_allowed_size < element_count * element_size {
&& self.maximum_allowed_size < u128::from(element_count) * u128::from(element_size) {
span_lint_and_help(
cx,
LARGE_STACK_ARRAYS,
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ define_Conf! {
/// Lint: LARGE_STACK_ARRAYS, LARGE_CONST_ARRAYS.
///
/// The maximum allowed size for arrays on the stack
(array_size_threshold: u64 = 512_000),
(array_size_threshold: u128 = 512_000),
/// Lint: VEC_BOX.
///
/// The size of the boxed type in bytes, where boxing in a `Vec` is allowed
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/crashes/ice-10044.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
[0; usize::MAX];
}
10 changes: 10 additions & 0 deletions tests/ui/crashes/ice-10044.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: statement with no effect
--> $DIR/ice-10044.rs:2:5
|
LL | [0; usize::MAX];
| ^^^^^^^^^^^^^^^^
|
= note: `-D clippy::no-effect` implied by `-D warnings`

error: aborting due to previous error

0 comments on commit 09cfcaf

Please sign in to comment.