rust-lang / rust-lang/rust-clippy
New lint: creation of huge array on the stack
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
The following code will segfault on playground due to stack overflow:
fn do_stuff() {
let data = [0; 10000000];
}
The problem is that Rust arrays are created on the stack, but this array is too large to fit on the stack.
What's worse, the naive solution doesn't work either:
fn do_stuff() {
let data = Box::new([0; 10000000]);
}
This still instantiates the array on the stack and segfaults. The proper solution is this:
fn do_stuff() {
let data = vec![0; 10000000].into_boxed_slice();
}
This issue is particularly tricky if the array size is dynamic, and does not typically manifest on tests, resulting in unexpected crashes in production. Example:
fn do_stuff(len: usize) {
let data = [0; len];
}
Here len can be set to an arbitrarily large number that would overflow the stack. Only length values of types u8, i8, u16, i16 are definitely safe. The solution is to use one of them or create the array on the heap as described above.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing rust-clippy's existing lint architecture and how it analyzes array creation expressions. Use the examples in the issue as the initial cases to investigate, including both constant and dynamic lengths. Done means a lint reliably identifies stack arrays that may be too large and covers the behavior with tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100