_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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

commit 28112e0208dea4374e6515240d8653d2a9af0caa
Author: Chouser <[EMAIL PROTECTED]>
Date:   Wed Nov 19 20:17:48 2008 -0500

    LazilyPersistentVector.toArray() now returns clone of array to prevent accidental mutation.

diff --git a/src/jvm/clojure/lang/LazilyPersistentVector.java b/src/jvm/clojure/lang/LazilyPersistentVector.java
index bbe580f..455ea42 100644
--- a/src/jvm/clojure/lang/LazilyPersistentVector.java
+++ b/src/jvm/clojure/lang/LazilyPersistentVector.java
@@ -35,7 +35,7 @@ LazilyPersistentVector(IPersistentMap meta, Object[] array, PersistentVector v){
 }
 
 public Object[] toArray(){
-	return array;
+	return array.clone();
 }
 
 public Object nth(int i){

Reply via email to