Skip to content

OrderedSet/hashCode throws an NPE if it contains nil #73

Closed
@NoahTheDuke

Description

@NoahTheDuke

Repo:

(.hashCode (os/ordered-set nil :a :b :c))

Stack trace:

#error {
 :cause nil
 :via
 [{:type java.lang.NullPointerException
   :message nil
   :at [flatland.ordered.set.OrderedSet$fn__4371 invoke "set.clj" 59]}]
 :trace
 [[flatland.ordered.set.OrderedSet$fn__4371 invoke "set.clj" 59]
  [clojure.core$map$fn__5935 invoke "core.clj" 2770]
  [clojure.lang.LazySeq sval "LazySeq.java" 42]
  [clojure.lang.LazySeq seq "LazySeq.java" 51]
  [clojure.lang.RT seq "RT.java" 535]
  [clojure.core$seq__5467 invokeStatic "core.clj" 139]
  [clojure.core.protocols$seq_reduce invokeStatic "protocols.clj" 24]
  [clojure.core.protocols$fn__8236 invokeStatic "protocols.clj" 75]
  [clojure.core.protocols$fn__8236 invoke "protocols.clj" 75]
  [clojure.core.protocols$fn__8178$G__8173__8191 invoke "protocols.clj" 13]
  [clojure.core$reduce invokeStatic "core.clj" 6882]
  [clojure.core$reduce invoke "core.clj" 6868]
  [flatland.ordered.set.OrderedSet hashCode "set.clj" 59]
...

Comes from unconditionally calling .hashCode on all of the contained items:

(reduce + (map #(.hashCode ^Object %) (.seq this))))

I believe this could be solved by either using clojure.core/hash or by changing to a keep (which is fine because the hash of nil is 0):

(reduce + (keep #(when % (.hashCode ^Object %)) (.seq this)))) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions