forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix rust-lang#121126: index out of bounds exceeds max value
When indexing an array with an index (u32) that exceeds the maximum value allowed by FieldIdx (default: 0xFFFF_FF00), although the compiler would detect the error, it would also cause a panic, which is a bug. I fixed it by adding a verification before calling the FieldIdx::from_u32(idx) method. This check ensures that if the idx value is greater than the maximum allowed value, it returns Option::None, similar to how other functions handle errors during the call to the project method of type Value.
- Loading branch information
Showing
3 changed files
with
31 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tests/ui/indexing/index-out-of-bounds-exceeds-max-value-issue-121126.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Regression test for #121126. Compiler was panicking when indexing an array | ||
// with an index that is out of bounds and its value is greater than the max | ||
// value allowed for an index. | ||
|
||
//@ build-fail | ||
|
||
fn main() { | ||
[0][0xFFFF_FF01]; | ||
//~^ ERROR this operation will panic at runtime [unconditional_panic] | ||
} | ||
|
||
// NOTE: In order for the test to be valid, the index can take on any value | ||
// between FieldIdx::MAX + 1 (= 0xFFF_FF01) and u32::MAX (= 0xFFF_FFFF) |
10 changes: 10 additions & 0 deletions
10
tests/ui/indexing/index-out-of-bounds-exceeds-max-value-issue-121126.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: this operation will panic at runtime | ||
--> $DIR/issue-121126-index-out-of-bounds-exceeds-max-value.rs:8:5 | ||
| | ||
LL | [0][0xFFFF_FF01]; | ||
| ^^^^^^^^^^^^^^^^ index out of bounds: the length is 1 but the index is 4294967041 | ||
| | ||
= note: `#[deny(unconditional_panic)]` on by default | ||
|
||
error: aborting due to 1 previous error | ||
|