Skip to content

Commit

Permalink
fix: bound check
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored and bobzhang committed Mar 22, 2024
1 parent 5cb42ba commit 84a2e99
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions deque/deque.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,11 @@ test "push_and_pop" {
/// println(dq[2]) // 3
/// ```
pub fn op_get[T](self : Deque[T], index : Int) -> T {
if index >= self.len {
if index < 0 || index >= self.len {
let len = self.len
abort("index out of bounds: the len is \(len) but the index is \(index)")
abort(
"index out of bounds: the len is from 0 to \(len) but the index is \(index)",
)
}
if self.head + index < self.buf.length() {
self.buf[self.head + index]
Expand Down Expand Up @@ -295,9 +297,11 @@ test "op_get" {
/// println(dq[2]) // 1
/// ```
pub fn op_set[T](self : Deque[T], index : Int, value : T) -> Unit {
if index >= self.len {
if index < 0 || index >= self.len {
let len = self.len
abort("index out of bounds: the len is \(len) but the index is \(index)")
abort(
"index out of bounds: the len is from 0 to \(len) but the index is \(index)",
)
}
if self.head + index < self.buf.length() {
self.buf[self.head + index] = value
Expand Down

0 comments on commit 84a2e99

Please sign in to comment.