Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_of_bounds_indexing doesn't apply to array references #11762

Closed
TetroGem opened this issue Nov 5, 2023 · 1 comment · Fixed by #11998
Closed

out_of_bounds_indexing doesn't apply to array references #11762

TetroGem opened this issue Nov 5, 2023 · 1 comment · Fixed by #11998
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@TetroGem
Copy link

TetroGem commented Nov 5, 2023

Summary

Array references with known length are not flagged when indexed out of bounds

Lint Name

out_of_bounds_indexing

Reproducer

The following correctly is flagged by clippy:

fn foo(nums: [u32; 3]) {
  nums[3]; // clippy: index out of bounds error
}

But the following is not:

fn foo(nums: &[u32; 3]) {
  nums[3]; // no error
}

Even though both have known sizes at compile time

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-pc-windows-msvc
release: 1.73.0
LLVM version: 17.0.2
@TetroGem TetroGem added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Nov 5, 2023
@cocodery
Copy link
Contributor

@rustbot claim

bors added a commit that referenced this issue Dec 22, 2023
Check whether out of bound when access a known length array with a constant index

fixes [Issue#11762](#11762)

Issue#11762 points that `Array references with known length are not flagged when indexed out of bounds`.

To fix this problem, it is needed to add check for `Expr::Index`. We expand this issue include reference and direct accessing a array.

When we access a array with a constant index `off`, and already know the length `size`, if `off >= size`, these code will throw an error, instead rustc's lint checking them or runtime panic happening.

changelog: [`out_of_bound_indexing`]: Add check for illegal accessing known length array with a constant index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
2 participants