Clojure Bugs Found: 17 Issues in Core via Lean 4

Clojure bugs found with Lean 4 and differential testing

A developer using Lean 4 and differential testing has turned up 17 previously unreported Clojure bugs in the language’s core, including a vector-of reduce call that never finishes. The Clojure bugs sit in the Java-written part of clojure.core, checked against Clojure 1.12.7, released on November 18, 2026, on JDK 21.

How the Clojure bugs were found

The method has two parts. For small functions built on integer and bit arithmetic, the developer modeled Java’s long and int as 64-bit and 32-bit bit vectors in Lean, then asked the bv_decide tactic to search for counterexamples. Each counterexample became a Clojure input that was run against the real code. Then the corrected logic got its own Lean model and proof.

Large persistent data structures don’t reduce to simple arithmetic, so those got differential testing. The same random operations ran against PersistentHashMap and java.util.HashMap, and vectors were compared with ArrayList around the sizes where the tree gains a level. To confirm the bugs were new, the developer queried Clojure’s JIRA through its REST API and searched ask.clojure.org. No report matched.

vector-of reduce never returns

The vector-of function builds a vector that keeps its values in primitive arrays. It lives in gvec.clj, apart from PersistentVector. Dropping one element and then calling reduce hangs once the vector reaches 65,505 elements. With 65,504 it returns a result.

The cause is a mask on line 79 of gvec.clj, 0xffe0, meant to round the next 32-element chunk start down to a multiple of 32. It also wipes bit 16 and everything above it. After the last chunk at index 65,504 the next start should be 65,536, but the mask turns it into 0, so reduce restarts and loops forever. The mask has been there since the 2010 commit that added internal-reduce. A mask of (bit-not 0x1f) passes the Lean proof.

dissoc on a hash collision node hides a key

Clojure’s hash-map walks a tree using 5-bit slices of each key’s hash. Keys with identical 32-bit hashes end up together in a HashCollisionNode. In HashCollisionNode.without, the new node is built with the hash parameter of the key passed to dissoc instead of this.hash.

That matters when a key equal to the stored one has a different hash. A Clojure vector equals a Java ArrayList with the same elements, but they hash differently, a mismatch tracked as CLJ-1372. In the demonstration, two ArrayList keys hashing to 1003 were stored, one was removed using a vector key, and then another assoc split the survivors into separate branches. The map printed both keys, yet get on one returned nil. The disj function on hash sets has the same flaw. The transient dissoc! path does not. Changing the line to this.hash fixed the example.

removeIf behaves differently with and without a type hint

Since Clojure 1.12, a Clojure function can be passed where a Java method expects a functional interface. When the compiler knows the type, calling removeIf with a set as the predicate throws a ClassCastException, because the adapter can’t turn the returned Long into a Boolean. Hide the type with identity and the call goes through reflection, where Reflector.coerceAdapterReturn still applies RT.booleanCast, and the elements are removed.

Integer overflow and a negative subvec index

The new IDrop implementations in Clojure 1.12 add the current position and n as ints and compare the sum with the length. In StringSeq.drop, calling drop with Integer/MAX_VALUE on the next seq of “abc” throws a StringIndexOutOfBoundsException. Lean found the counterexample i = 2147483646, n = 2147483647. Comparing n against the remaining length proved correct for every 32-bit input.

Separately, SubVector.assocN doesn’t reject a negative index. Calling assoc with -1 on a subvec overwrites an element of the underlying vector outside the subvec and returns a subvec that looks unchanged. The same bug in nth was fixed under CLJ-962, but assoc kept it.

Where nothing broke

The core trees held up well. PersistentHashMap and PersistentHashSet matched java.util.HashMap across 3 million mixed persistent and transient operations, including collision nodes down to depth 7. PersistentTreeMap matched java.util.TreeMap and kept its red-black invariants. Vectors matched ArrayList at every boundary tested. The defects turned up in code outside the trees and at the seams between mechanisms, such as equality and hashing.

Lean also proved the check Numbers uses to pick the fast path for addition and subtraction agrees with the overflow check at 64 bits. Multiplication timed out at 16 bits, so it was checked at 8 and 10 bits, and clojure.core/mod matched the floored remainder up to 32 bits.

What Clojure maintainers can do next

The remaining 12 bugs matter less, though each reproduces on 1.12.7. With reproductions and tested fixes in hand, the next step is for the Clojure team to review and file them in JIRA. The approach could also be reused on other mature codebases, as long as the targets are small integer-heavy functions or structures that can be compared against a trusted reference.

Source: Conao3 Notes

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.