Skip to content

Fix NPE in ordered-set #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/flatland/ordered/set.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
(toString [this]
(str "#{" (clojure.string/join " " (map str this)) "}"))
(hashCode [this]
(reduce + (map #(.hashCode ^Object %) (.seq this))))
(reduce + (keep #(when % (.hashCode ^Object %)) (.seq this))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look good to me, since false will be treated the same way here as nil

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Highly recomended to add an explicit test case with false as an element of a set, to confirm, and then fix the expected issue that such a test case should find.

(equals [this other]
(or (identical? this other)
(and (instance? Set other)
Expand Down
7 changes: 5 additions & 2 deletions test/flatland/ordered/set_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,14 @@
(is (= (hash m1) (hash m2)))
(is (= (.hashCode m1) (.hashCode m2)))
(is (= (hash (ordered-set)) (hash (hash-set))))
(is (= (.hashCode (ordered-set)) (.hashCode (hash-set))))))
(is (= (.hashCode (ordered-set)) (.hashCode (hash-set))))
(is (= (hash (ordered-set nil)) (hash (hash-set nil))))
(is (= (.hashCode (ordered-set nil)) (.hashCode (hash-set nil))))
(is (= (.hashCode (ordered-set nil :a {:b nil})) (.hashCode (hash-set nil :a {:b nil}))))))

(deftest nil-hash-code-npe
;; No assertions here; just check that it doesn't NPE
;; See: https://github.com/amalloy/ordered/issues/27
(are [contents] (.hashCode (ordered-set contents))
(are [contents] (.hashCode (apply ordered-set contents))
[nil]
[nil :a]))