apache / apache/lucene

AssertionError in BooleanQuery.hashCode() [LUCENE-10431]

Open
#11,467 17 comments 0 reactions 0 assignees View on GitHub
affects-version:8.11.1 legacy-jira-priority:Major type:bug
Dominant language
Java
Stars
3.6k
Forks
1.4k
Avg merge
2d 11h
Merged PRs (30d)
88

Description

Hello devs,

the constructor of BooleanQuery can under some circumstances trigger a hash code computation before "clauseSets" is fully filled. Since BooleanClause is using its query field for the hash code too, it can happen that the "wrong" hash code is stored, since adding the clause to the set triggers its hashCode().

If assertions are enabled the check in BooleanQuery, which recomputes the hash code, will notice it and throw an error.

exception:

```java
java.lang.AssertionError
    at org.apache.lucene.search.BooleanQuery.hashCode(BooleanQuery.java:614)
    at java.base/java.util.Objects.hashCode(Objects.java:103)
    at java.base/java.util.HashMap$Node.hashCode(HashMap.java:298)
    at java.base/java.util.AbstractMap.hashCode(AbstractMap.java:527)
    at org.apache.lucene.search.Multiset.hashCode(Multiset.java:119)
    at java.base/java.util.EnumMap.entryHashCode(EnumMap.java:717)
    at java.base/java.util.EnumMap.hashCode(EnumMap.java:709)
    at java.base/java.util.Arrays.hashCode(Arrays.java:4498)
    at java.base/java.util.Objects.hash(Objects.java:133)
    at org.apache.lucene.search.BooleanQuery.computeHashCode(BooleanQuery.java:597)
    at org.apache.lucene.search.BooleanQuery.hashCode(BooleanQuery.java:611)
    at java.base/java.util.HashMap.hash(HashMap.java:340)
    at java.base/java.util.HashMap.put(HashMap.java:612)
    at org.apache.lucene.search.Multiset.add(Multiset.java:82)
    at org.apache.lucene.search.BooleanQuery.(BooleanQuery.java:154)
    at org.apache.lucene.search.BooleanQuery.(BooleanQuery.java:42)
    at org.apache.lucene.search.BooleanQuery$Builder.build(BooleanQuery.java:133)
```

I noticed this while trying to upgrade the NetBeans maven indexer modules from lucene 5.x to 8.x https://github.com/apache/netbeans/pull/3558

---
Migrated from [LUCENE-10431](https://issues.apache.org/jira/browse/LUCENE-10431) by Michael Bien (@mbien), updated Mar 04 2022
Pull requests: https://github.com/apache/lucene/pull/722, https://github.com/apache/lucene/pull/727

Contributor guide

Open the contributing guide

Research direction

Start in org.apache.lucene.search.BooleanQuery, especially BooleanQuery.java around hashCode(), computeHashCode(), and the constructors, then inspect Multiset.java where the stack trace shows the hash is triggered. Compare the issue with pull requests 722 and 727; done means the reported assertion no longer occurs when BooleanQuery clauses are built and hashed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
search
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.