Skip to content

Commit

Permalink
CLJ-2839 Infinite seq class hashCode() is infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
puredanger committed Mar 7, 2024
1 parent ce55092 commit 777456f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/clj/clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3028,7 +3028,8 @@
[n x] (take n (repeat x)))

(defn iterate
"Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects"
"Returns a lazy (infinite!) sequence of x, (f x), (f (f x)) etc.
f must be free of side-effects"
{:added "1.0"
:static true}
[f x] (clojure.lang.Iterate/create f x) )
Expand Down
9 changes: 9 additions & 0 deletions src/jvm/clojure/lang/Cycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,13 @@ public Object reduce(IFn f, Object start){
s = all;
}
}

public int hashCode(){
throw new UnsupportedOperationException();
}

public int hasheq(){
throw new UnsupportedOperationException();
}

}
8 changes: 8 additions & 0 deletions src/jvm/clojure/lang/Iterate.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ public Object reduce(IFn rf, Object start){
v = f.invoke(v);
}
}

public int hashCode(){
throw new UnsupportedOperationException();
}

public int hasheq(){
throw new UnsupportedOperationException();
}
}
13 changes: 13 additions & 0 deletions src/jvm/clojure/lang/Repeat.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,17 @@ public Object reduce(IFn f, Object start){
}
}

public int hashCode(){
if(count <= 0)
throw new UnsupportedOperationException();
else
return super.hashCode();
}

public int hasheq(){
if(count <= 0)
throw new UnsupportedOperationException();
else
return super.hasheq();
}
}
10 changes: 10 additions & 0 deletions test/clojure/test_clojure/sequences.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,16 @@
:kf #(some-> % :k #{0 1 2})
:vf :item))))))

(deftest infinite-seq-hash
(are [e] (thrown? Exception (.hashCode ^Object e))
(iterate identity nil)
(cycle [1])
(repeat 1))
(are [e] (thrown? Exception (.hasheq ^clojure.lang.IHashEq e))
(iterate identity nil)
(cycle [1])
(repeat 1)))

(defspec iteration-seq-equals-reduce 1000
(prop/for-all [initk gen/int
seed gen/int]
Expand Down
9 changes: 8 additions & 1 deletion test/clojure/test_clojure/serialization.clj
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,11 @@

;; stateful seqs
(enumeration-seq (java.util.Collections/enumeration (range 50)))
(iterator-seq (.iterator (range 50)))))
(iterator-seq (.iterator (range 50)))))

;; necessary for CVE-2024-22871
(deftest CLJ-2839
(are [e] (thrown? Exception (.hashCode ^Object (-> e serialize deserialize)))
(repeat 1)
(iterate identity nil)
(cycle [1])))

0 comments on commit 777456f

Please sign in to comment.