Auto merge of #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 #10044

changelog: Fix: [`large_const_arrays`] and [`large_stack_arrays`]: avoid integer overflow when calculating total array size
#10103
This commit is contained in:
bors 2022-12-19 16:43:32 +00:00
commit 09cfcaf693
5 changed files with 20 additions and 7 deletions

View file

@ -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 }
}
}
@ -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);

View file

@ -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 }
}
}
@ -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,

View file

@ -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

View file

@ -0,0 +1,3 @@
fn main() {
[0; usize::MAX];
}

View file

@ -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