-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix IntegerOverflow exception in postings encoding as group-varint (#…
…13376) The exception happen because the tail postings list block, which encoding with GroupVInt, had a docID delta that was >= 1<<30, when the postings are also storing freqs.
- Loading branch information
Showing
8 changed files
with
182 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
lucene/core/src/java/org/apache/lucene/codecs/lucene99/PostingsUtil.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.lucene99; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.store.IndexInput; | ||
import org.apache.lucene.store.IndexOutput; | ||
|
||
/** Utility class to encode/decode postings block. */ | ||
final class PostingsUtil { | ||
|
||
/** | ||
* Read values that have been written using variable-length encoding and group-varint encoding | ||
* instead of bit-packing. | ||
*/ | ||
static void readVIntBlock( | ||
IndexInput docIn, | ||
long[] docBuffer, | ||
long[] freqBuffer, | ||
int num, | ||
boolean indexHasFreq, | ||
boolean decodeFreq) | ||
throws IOException { | ||
docIn.readGroupVInts(docBuffer, num); | ||
if (indexHasFreq && decodeFreq) { | ||
for (int i = 0; i < num; ++i) { | ||
freqBuffer[i] = docBuffer[i] & 0x01; | ||
docBuffer[i] >>= 1; | ||
if (freqBuffer[i] == 0) { | ||
freqBuffer[i] = docIn.readVInt(); | ||
} | ||
} | ||
} else if (indexHasFreq) { | ||
for (int i = 0; i < num; ++i) { | ||
docBuffer[i] >>= 1; | ||
} | ||
} | ||
} | ||
|
||
/** Write freq buffer with variable-length encoding and doc buffer with group-varint encoding. */ | ||
static void writeVIntBlock( | ||
IndexOutput docOut, long[] docBuffer, long[] freqBuffer, int num, boolean writeFreqs) | ||
throws IOException { | ||
if (writeFreqs) { | ||
for (int i = 0; i < num; i++) { | ||
docBuffer[i] = (docBuffer[i] << 1) | (freqBuffer[i] == 1 ? 1 : 0); | ||
} | ||
} | ||
docOut.writeGroupVInts(docBuffer, num); | ||
if (writeFreqs) { | ||
for (int i = 0; i < num; i++) { | ||
final int freq = (int) freqBuffer[i]; | ||
if (freq != 1) { | ||
docOut.writeVInt(freq); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
50 changes: 50 additions & 0 deletions
50
lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestPostingsUtil.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.lucene.codecs.lucene99; | ||
|
||
import java.io.IOException; | ||
import org.apache.lucene.store.Directory; | ||
import org.apache.lucene.store.IOContext; | ||
import org.apache.lucene.store.IndexInput; | ||
import org.apache.lucene.store.IndexOutput; | ||
import org.apache.lucene.tests.util.LuceneTestCase; | ||
import org.apache.lucene.tests.util.TestUtil; | ||
|
||
public class TestPostingsUtil extends LuceneTestCase { | ||
|
||
// checks for bug described in https://github.com/apache/lucene/issues/13373 | ||
public void testIntegerOverflow() throws IOException { | ||
final int size = TestUtil.nextInt(random(), 1, ForUtil.BLOCK_SIZE); | ||
final long[] docDeltaBuffer = new long[size]; | ||
final long[] freqBuffer = new long[size]; | ||
|
||
final int delta = 1 << 30; | ||
docDeltaBuffer[0] = delta; | ||
try (Directory dir = newDirectory()) { | ||
try (IndexOutput out = dir.createOutput("test", IOContext.DEFAULT)) { | ||
// In old implementation, this would cause integer overflow exception. | ||
PostingsUtil.writeVIntBlock(out, docDeltaBuffer, freqBuffer, size, true); | ||
} | ||
long[] restoredDocs = new long[size]; | ||
long[] restoredFreqs = new long[size]; | ||
try (IndexInput in = dir.openInput("test", IOContext.DEFAULT)) { | ||
PostingsUtil.readVIntBlock(in, restoredDocs, restoredFreqs, size, true, true); | ||
} | ||
assertEquals(delta, restoredDocs[0]); | ||
} | ||
} | ||
} |
This file contains 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