Skip to content

Commit 9887580

Browse files
ducky427dnolen
authored andcommitted
CLJS-1649: Possible issue with in cljs.reader or cljs.core/PersistentHashMap.
We are storing last 256 computed hash-string values in a JS object. JS objects only accept strings as keys. So its not able to distinguish between a `nil` and a `"null"` if either is present in the cache already. We are already assuming that `nil` hashes to 0. So don't check for it in the cache or try to compute its hash.
1 parent fc827fa commit 9887580

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -871,10 +871,12 @@
871871
(when (> string-hash-cache-count 255)
872872
(set! string-hash-cache (js-obj))
873873
(set! string-hash-cache-count 0))
874-
(let [h (aget string-hash-cache k)]
875-
(if (number? h)
876-
h
877-
(add-to-string-hash-cache k))))
874+
(if (nil? k)
875+
0
876+
(let [h (aget string-hash-cache k)]
877+
(if (number? h)
878+
h
879+
(add-to-string-hash-cache k)))))
878880

879881
(defn hash
880882
"Returns the hash code of its argument. Note this is the hash code

src/test/cljs/cljs/core_test.cljs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,6 +3165,10 @@
31653165
(+ acc x)))
31663166
[2 4 6 :stop 8 10]))))
31673167

3168+
(deftest test-nil-hashing-cljs-1649
3169+
(is (zero? (hash-string nil)))
3170+
(is (not (zero? (hash-string "null")))))
3171+
31683172
(comment
31693173
;; ObjMap
31703174
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)