Skip to content

Commit b09ba58

Browse files
committed
add assumption check
1 parent fb4de0f commit b09ba58

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/Trie.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ long saveArcs(Node node, IndexOutput index, long startFP) throws IOException {
142142
| (output.hasTerms ? (1 << 5) : 0)
143143
| (output.floorData != null ? (1 << 6) : 0);
144144
index.writeByte(((byte) header));
145-
writeLongNBytes(output.fp, outputFpBytes, index);
145+
writeFpNBytes(output.fp, outputFpBytes, index);
146146
if (output.floorData != null) {
147147
index.writeBytes(output.floorData.bytes, output.floorData.offset, output.floorData.length);
148148
}
@@ -173,14 +173,14 @@ long saveArcs(Node node, IndexOutput index, long startFP) throws IOException {
173173
int header = SIGN_SINGLE_CHILDREN | (childFpBytes << 2) | (encodedOutputFpBytes << 5);
174174
index.writeByte((byte) header);
175175
index.writeByte((byte) node.children.getFirst().label);
176-
writeLongNBytes(fpBuffer[0], childFpBytes, index);
176+
writeFpNBytes(fpBuffer[0], childFpBytes, index);
177177
if (node.output != null) {
178178
Output output = node.output;
179179
long encodedFp =
180180
(output.floorData != null ? 0x01L : 0)
181181
| (output.hasTerms ? 0x02L : 0)
182182
| (output.fp << 2);
183-
writeLongNBytes(encodedFp, encodedOutputFpBytes, index);
183+
writeFpNBytes(encodedFp, encodedOutputFpBytes, index);
184184
if (output.floorData != null) {
185185
index.writeBytes(
186186
output.floorData.bytes, output.floorData.offset, output.floorData.length);
@@ -231,7 +231,7 @@ long saveArcs(Node node, IndexOutput index, long startFP) throws IOException {
231231
Output output = node.output;
232232
long encodedFp =
233233
(output.floorData != null ? 0x01L : 0) | (output.hasTerms ? 0x02L : 0) | (output.fp << 2);
234-
writeLongNBytes(encodedFp, encodedOutputFpBytes, index);
234+
writeFpNBytes(encodedFp, encodedOutputFpBytes, index);
235235
}
236236

237237
long positionStartFp = index.getFilePointer();
@@ -244,7 +244,7 @@ long saveArcs(Node node, IndexOutput index, long startFP) throws IOException {
244244
+ (index.getFilePointer() - positionStartFp);
245245

246246
for (int i = 0; i < childrenNum; i++) {
247-
writeLongNBytes(fpBuffer[i], childrenFpBytes, index);
247+
writeFpNBytes(fpBuffer[i], childrenFpBytes, index);
248248
}
249249

250250
if (node.output != null && node.output.floorData != null) {
@@ -259,7 +259,11 @@ private static int bytesRequired(long v) {
259259
return Math.max(1, Long.BYTES - (Long.numberOfLeadingZeros(v) >>> 3));
260260
}
261261

262-
private static void writeLongNBytes(long v, int n, DataOutput out) throws IOException {
262+
private static void writeFpNBytes(long v, int n, DataOutput out) throws IOException {
263+
if (n > 7) {
264+
throw new IllegalArgumentException(
265+
"term dictionary can not have file pointers bigger than 2^56, got: " + v);
266+
}
263267
for (int i = 0; i < n; i++) {
264268
out.writeByte((byte) v);
265269
v >>= 8;

lucene/core/src/java/org/apache/lucene/codecs/lucene90/blocktree/TrieReader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ private void load(Node node, long fp) throws IOException {
8383

8484
// [n bytes] floor data
8585
// [n bytes] output fp
86-
// [1bit] nothing | [1bit] has floor | [1bit] has terms | [3bit] output fp bytes | [2bit]
86+
// [1bit] nothing | [1bit] has floor | [1bit] has terms | [3bit] output fp bytes | [2bit]
8787
// sign
8888

8989
node.childrenNum = 0;
9090
int fpBytes = (term >>> 2) & 0x07;
91-
node.outputFp =
92-
(termLong >>> 8) & bytesAsMask(fpBytes); // assumption fp for tip less than 56 bit
91+
node.outputFp = (termLong >>> 8) & bytesAsMask(fpBytes);
9392
node.hasTerms = (term & 0x20) != 0;
9493
if ((term & 0x40) != 0) {
9594
node.floorDataFp = fp + 1 + fpBytes;
@@ -104,13 +103,13 @@ private void load(Node node, long fp) throws IOException {
104103

105104
// [n bytes] floor data
106105
// [n bytes] encoded output fp | [n bytes] child fp | [1 byte] label
107-
// [3bit] encoded output fp bytes | [3bit] child fp bytes | | [2bit] sign
106+
// [3bit] encoded output fp bytes | [3bit] child fp bytes | [2bit] sign
108107

109108
node.childrenNum = 1;
110109
int childFpBytes = (term >>> 2) & 0x07;
111110
int encodedOutputFpBytes = (term >>> 5) & 0x07;
112-
node.childFp =
113-
(termLong >>> 16) & bytesAsMask(childFpBytes); // assumption fp for tip less than 48 bit
111+
long l = childFpBytes <= 6 ? termLong >>> 16 : access.readLong(fp + 2);
112+
node.childFp = l & bytesAsMask(childFpBytes);
114113
node.minChildrenLabel = (term >>> 8) & 0xFF;
115114

116115
if (encodedOutputFpBytes == 0) {

lucene/core/src/test/org/apache/lucene/codecs/lucene90/blocktree/TestTrie.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public void testTrie() {
4242
BytesRef key = new BytesRef(randomBytes());
4343
Trie.Output value =
4444
new Trie.Output(
45-
random().nextLong(Integer.MAX_VALUE),
46-
random().nextBoolean(),
47-
new BytesRef(randomBytes()));
45+
random().nextLong(1L << 50), random().nextBoolean(), new BytesRef(randomBytes()));
4846
expected.put(key, value);
4947
trie.putAll(new Trie(key, value));
5048
}
@@ -66,7 +64,7 @@ public void testTrieLookup() throws IOException {
6664
BytesRef key = new BytesRef(randomBytes());
6765
Trie.Output value =
6866
new Trie.Output(
69-
random().nextLong(Integer.MAX_VALUE),
67+
random().nextLong(1L << 50),
7068
random().nextBoolean(),
7169
random().nextBoolean() ? null : new BytesRef(randomBytes()));
7270
expected.put(key, value);

0 commit comments

Comments
 (0)