mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
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:
commit
09cfcaf693
5 changed files with 20 additions and 7 deletions
|
@ -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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
3
tests/ui/crashes/ice-10044.rs
Normal file
3
tests/ui/crashes/ice-10044.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
[0; usize::MAX];
|
||||
}
|
10
tests/ui/crashes/ice-10044.stderr
Normal file
10
tests/ui/crashes/ice-10044.stderr
Normal 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
|
||||
|
Loading…
Reference in a new issue