Skip to content

Commit 18f6d46

Browse files
authored
Merge pull request #6 from codeboost/bound-check-fix
Throw exception if requested index is out of bounds
2 parents 87ccef7 + 0afa8cf commit 18f6d46

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/xitdb/array_list.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@
4040

4141
clojure.lang.Indexed
4242
(nth [_ i]
43-
(let [cursor (.getCursor ral (long i))]
44-
(common/-read-from-cursor cursor)))
43+
(let [count (.count ral)
44+
idx (long i)]
45+
(if (and (>= idx 0) (< idx count))
46+
(common/-read-from-cursor (.getCursor ral idx))
47+
(throw (IndexOutOfBoundsException. (str "Index: " i ", Size: " count))))))
4548

4649
(nth [_ i not-found]
4750
(let [cursor (.getCursor ral (long i))]

src/xitdb/util/operations.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
cursor (.getCursor rhs hash-code)]
186186
(some? cursor)))
187187

188-
(defn ^WriteHashMap set-empty!
188+
(defn ^WriteHashSet set-empty!
189189
"Replaces the whs value with an empty set."
190190
[^WriteHashSet whs]
191191
(let [empty-set (conversion/v->slot! (.cursor whs) #{})]

test/xitdb/cursor_test.clj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@
2424
(swap! cursor1 assoc-in [3 :hidden] :changed-by-swap!)
2525
(is (= [1 2 3 {:hidden :changed-by-swap!} 5]) (xdb/materialize @cursor1))
2626
(is (= :changed-by-swap! @cursor3))
27-
(is (= 3 @cursor2))))))
27+
(is (= 3 @cursor2)))
28+
29+
(testing "Correctly handles invalid cursor path"
30+
(is (thrown? IndexOutOfBoundsException @(xdb/xdb-cursor db [:foo :bar 999])))))))

0 commit comments

Comments
 (0)