Skip to content

Commit 09cfcaf

Browse files
committed
Auto merge of rust-lang#10103 - Niki4tap:overflow_ice, r=flip1995
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
2 parents b3145fe + d0ac6ba commit 09cfcaf

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

clippy_lints/src/large_const_arrays.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ declare_clippy_lint! {
3434
}
3535

3636
pub struct LargeConstArrays {
37-
maximum_allowed_size: u64,
37+
maximum_allowed_size: u128,
3838
}
3939

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

6161
then {
6262
let hi_pos = item.ident.span.lo() - BytePos::from_usize(1);

clippy_lints/src/large_stack_arrays.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ declare_clippy_lint! {
2424
}
2525

2626
pub struct LargeStackArrays {
27-
maximum_allowed_size: u64,
27+
maximum_allowed_size: u128,
2828
}
2929

3030
impl LargeStackArrays {
3131
#[must_use]
32-
pub fn new(maximum_allowed_size: u64) -> Self {
32+
pub fn new(maximum_allowed_size: u128) -> Self {
3333
Self { maximum_allowed_size }
3434
}
3535
}
@@ -45,7 +45,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
4545
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
4646
&& !cx.tcx.hir().parent_iter(expr.hir_id)
4747
.any(|(_, node)| matches!(node, Node::Item(Item { kind: ItemKind::Static(..), .. })))
48-
&& self.maximum_allowed_size < element_count * element_size {
48+
&& self.maximum_allowed_size < u128::from(element_count) * u128::from(element_size) {
4949
span_lint_and_help(
5050
cx,
5151
LARGE_STACK_ARRAYS,

clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ define_Conf! {
333333
/// Lint: LARGE_STACK_ARRAYS, LARGE_CONST_ARRAYS.
334334
///
335335
/// The maximum allowed size for arrays on the stack
336-
(array_size_threshold: u64 = 512_000),
336+
(array_size_threshold: u128 = 512_000),
337337
/// Lint: VEC_BOX.
338338
///
339339
/// The size of the boxed type in bytes, where boxing in a `Vec` is allowed

tests/ui/crashes/ice-10044.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
[0; usize::MAX];
3+
}

tests/ui/crashes/ice-10044.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: statement with no effect
2+
--> $DIR/ice-10044.rs:2:5
3+
|
4+
LL | [0; usize::MAX];
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::no-effect` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)