Bug + Patch: LazilyPersistentVector mutated by sort

0 views
Skip to first unread message

Chouser

unread,
Nov 19, 2008, 8:23:01 PM11/19/08
to clo...@googlegroups.com
_Jordan_ in IRC discovered a bug that can be distilled to:

user=> (let [x [3 2 1]] (sort x) x)
[1 2 3]

The sort function mutates the vector. This can happen because the
toArray() method of LazilyPersistenVector returns its own internal
array, instead of creating a new array like most other collections do.
sort then passes this along to Java's Arrays.sort() which mutates the
array.

Attached is a patch that makes LPV toArray() return a clone of the
array. This is of course somewhat less efficient, but it seems like
any less drastic solution leaves open the possibility of mutating the
array inside the vector.

--Chouser

fix-lpvector.patch

Rich Hickey

unread,
Nov 19, 2008, 8:46:48 PM11/19/08
to Clojure
Patch applied (rev 1114) - thanks!

Rich
Reply all
Reply to author
Forward
0 new messages