Welcome! Please see the About page for a little more info on how this works.

0 votes
ago in Sequences by

Clojure 1.12.7, JDK 21 (regression from 1.11.4; same on master).

drop (and nthnext, nthrest) on a non-initial string seq or vector seq overflows int when n is close to Integer/MAX_VALUE, returning a broken seq at a negative index or throwing IndexOutOfBoundsException.

Expected: () (and nil for nthnext), matching Clojure 1.11.4 and the other IDrop implementations (PersistentVector, PersistentArrayMap, LongRange, Repeat).

(count (drop Integer/MAX_VALUE (next (seq "abc"))))
;; Actual: -2147483645
;; Expected: 0

(drop Integer/MAX_VALUE (next (seq "abc")))
;; Actual: printing throws StringIndexOutOfBoundsException: Index -2147483648
;; Expected: ()

(drop Integer/MAX_VALUE (next (seq [1 2 3])))
;; Actual: printing throws ArrayIndexOutOfBoundsException: Index -2147483648
;; Expected: ()

(drop (- Integer/MAX_VALUE 10) (nthnext (seq (vec (range 40))) 32))
;; Actual: IndexOutOfBoundsException
;; Expected: ()

Cause: StringSeq.drop and PersistentVector.ChunkedSeq.drop add the current position and n as 32-bit ints (i + n, offset + n, and i + offset + n) before comparing with the length, so the sum wraps negative and passes the < check. Comparing n with the remaining length keeps the check in range.

This is separate from CLJ-2787, where n itself doesn't fit in an int.

This bug is reported at https://s.conao3.com/blog/2026/e4f733

Please log in or register to answer this question.

...