Declare equals/hashCode consistent with compareTo (CodeQL java/inconsistent-compareto-and-equals)#232
Open
vharseko wants to merge 2 commits into
Conversation
…istent-compareto-and-equals) Three persistit engine classes implemented Comparable/compareTo but inherited Object.equals, so equals and compareTo could disagree: - BufferPool.BufferHolder (sorted via Arrays.sort by volumeId, page) - JournalManager.TransactionMapItem (held in a TreeSet, ordered by timestamp) - TreeBuilder.Node (keyed in a TreeMap, ordered by tree + key) None of these types is currently compared through equals/hashCode - the sorted collections (TreeMap, TreeSet, Arrays.sort) all use compareTo, and the types are never placed in a hash collection keyed by themselves nor looked up via contains/indexOf/remove(Object). The inconsistency was therefore latent, but CodeQL flags it and it is a real trap for future callers. Add equals and hashCode to each class, defined so that two instances are equal exactly when compareTo returns 0, and hashCode is derived from the same ordering key. Because nothing relied on the inherited identity equals, this is behaviour-preserving for all existing usages. Also add the missing 3A Systems Portions header line to JournalManager.java (TreeBuilder.java and BufferPool.java already carry it).
maximthomas
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Resolves all three open
java/inconsistent-compareto-and-equalsCodeQL alertsin the persistit engine. Each flagged class implements
Comparable/compareTobut inherited
Object.equals, soequalsandcompareTocould beinconsistent:
compareToorders byBufferPool.BufferHoldervolumeId,pageArrays.sortJournalManager.TransactionMapItemTreeSet, sortedListTreeBuilder.NodekeyTreeMap<Node, Node>Why it is safe
None of these types is currently compared through
equals/hashCode:TreeMap,TreeSet,Arrays.sort,Collections.sort) all usecompareTo, notequals;(
TransactionMapItemis only a value in aHashMap<Long, …>), andcontains/indexOf/remove(Object)call that would invokeequalson them.So the inconsistency was latent rather than an active bug, but it is exactly
the trap this rule guards against for future callers.
Fix
For each class, declare
equalsandhashCodeconsistent with the existingcompareTo:equals(o)returns true iffois the same reference orcompareTo(o) == 0;hashCodeis derived from the same ordering key(
(volumeId, page); commit-or-start timestamp; the tree instance for aNode).Because nothing relied on the inherited identity
equals, behaviour isunchanged for every existing usage.
Also adds the missing
Portions Copyrighted 2026 3A Systems, LLCheader lineto
JournalManager.java(TreeBuilder.javaandBufferPool.javaalreadycarry it).
Testing
mvn -o -pl persistit/core -am compile— BUILD SUCCESS.