Array of 100000 or more elements built on the stack

ID

rust.performance.large_stack_arrays

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

low

Resource

Inefficient Api Usage

Language

Rust

Description

Reports an array repeat expression with at least 100000 elements written inside a function body, where it is a local and therefore lives on the stack.

Rationale

A local array is laid out in the stack frame and initialised element by element on entry, so a large one both costs the initialisation on every call and eats a fixed slice of the stack. The main thread usually has 8 MB, but a spawned thread defaults to 2 MB and embedded targets far less, so an array that is comfortable in a test can overflow the stack in a worker - and a stack overflow aborts the process rather than returning an error.

The following code illustrates the pattern detected by this rule:

pub fn zeroed_page() -> u8 {
    // FLAGGED: Array of 100000 or more elements built on the stack
    let buffer = [0u8; 1048576];
    buffer[0]
}

Remediation

Move the storage to the heap with vec![0u8; n] or Box<[T]>, which allocates once and leaves the frame small. When the contents are fixed and read-only, make it a const or static instead, so it lives in the binary rather than being rebuilt per call.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved