From 84a2e99527917e96ba2b2a6217c2495b617c629f Mon Sep 17 00:00:00 2001 From: Lampese Date: Wed, 20 Mar 2024 22:53:38 +0800 Subject: [PATCH] fix: bound check --- deque/deque.mbt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/deque/deque.mbt b/deque/deque.mbt index ef6800b0e..44b0fdb3c 100644 --- a/deque/deque.mbt +++ b/deque/deque.mbt @@ -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] @@ -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