-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix IntegerOverflow exception in postings encoding as group-varint #13376
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b43c96c
Fix IntegerOverflow exception in postings encoding as group-vint
easyice d8cd000
iter
easyice dcaa4e8
iter
easyice 20f684b
add tests
easyice 3aff63d
Merge branch 'main' into fix_IntegerOverflow
easyice f631d45
update tests
easyice 42fe526
fix review
easyice c5a1b94
return positive long when reading the tail vints
easyice 59e8033
fix review
easyice 616d0d1
clean
easyice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
// This file has been automatically generated, DO NOT EDIT | ||
|
||
/* | ||
* 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] = (int) docBuffer[i] >>> 1; | ||
if (freqBuffer[i] == 0) { | ||
freqBuffer[i] = docIn.readVInt(); | ||
} | ||
} | ||
} else if (indexHasFreq) { | ||
for (int i = 0; i < num; ++i) { | ||
docBuffer[i] = (int) 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] = (int) (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
48 changes: 48 additions & 0 deletions
48
lucene/core/src/test/org/apache/lucene/codecs/lucene99/TestPostingUtil.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,48 @@ | ||
/* | ||
* 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; | ||
|
||
public class TestPostingUtil extends LuceneTestCase { | ||
|
||
// checks for bug described in https://github.com/apache/lucene/issues/13373 | ||
public void testIntegerOverflow() throws IOException { | ||
final long[] docDeltaBuffer = new long[ForUtil.BLOCK_SIZE]; | ||
final long[] freqBuffer = new long[ForUtil.BLOCK_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, ForUtil.BLOCK_SIZE, true); | ||
} | ||
long[] restoredDocs = new long[ForUtil.BLOCK_SIZE]; | ||
long[] restoredFreqs = new long[ForUtil.BLOCK_SIZE]; | ||
try (IndexInput in = dir.openInput("test", IOContext.DEFAULT)) { | ||
PostingsUtil.readVIntBlock(in, restoredDocs, restoredFreqs, ForUtil.BLOCK_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
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 |
---|---|---|
|
@@ -1442,6 +1442,19 @@ public void testListAllIsSorted() throws IOException { | |
} | ||
} | ||
|
||
public void testGroupVIntOverflow() throws IOException { | ||
try (Directory dir = getDirectory(createTempDir("testGroupVIntOverflow"))) { | ||
final int v = 1 << 30; | ||
final long[] values = new long[4]; | ||
values[0] = v; | ||
values[0] <<= 1; // values[0] = 2147483648 as long, but as int it is -2147483648 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
IndexOutput out = dir.createOutput("test", IOContext.DEFAULT); | ||
assertThrows(ArithmeticException.class, () -> out.writeGroupVInts(values, 4)); | ||
out.close(); | ||
} | ||
} | ||
|
||
public void testDataTypes() throws IOException { | ||
final long[] values = new long[] {43, 12345, 123456, 1234567890}; | ||
try (Directory dir = getDirectory(createTempDir("testDataTypes"))) { | ||
|
@@ -1468,18 +1481,21 @@ public void testDataTypes() throws IOException { | |
public void testGroupVInt() throws IOException { | ||
try (Directory dir = getDirectory(createTempDir("testGroupVInt"))) { | ||
// test fallback to default implementation of readGroupVInt | ||
doTestGroupVInt(dir, 5, 1, 6, 8); | ||
doTestGroupVInt(dir, 5, 0, 1, 6, 8); | ||
doTestGroupVInt(dir, 5, -8, 3, 3, 8); | ||
|
||
// use more iterations to covers all bpv | ||
doTestGroupVInt(dir, atLeast(100), 1, 31, 128); | ||
doTestGroupVInt(dir, atLeast(100), 0, 1, 31, 128); | ||
doTestGroupVInt(dir, 5, Integer.MIN_VALUE, 31, 31, 128); | ||
|
||
// we use BaseChunkedDirectoryTestCase#testGroupVIntMultiBlocks cover multiple blocks for | ||
// ByteBuffersDataInput and MMapDirectory | ||
} | ||
} | ||
|
||
protected void doTestGroupVInt( | ||
Directory dir, int iterations, int minBpv, int maxBpv, int maxNumValues) throws IOException { | ||
Directory dir, int iterations, int minValue, int minBpv, int maxBpv, int maxNumValues) | ||
throws IOException { | ||
long[] values = new long[maxNumValues]; | ||
int[] numValuesArray = new int[iterations]; | ||
IndexOutput groupVIntOut = dir.createOutput("group-varint", IOContext.DEFAULT); | ||
|
@@ -1490,7 +1506,8 @@ protected void doTestGroupVInt( | |
final int bpv = TestUtil.nextInt(random(), minBpv, maxBpv); | ||
numValuesArray[iter] = TestUtil.nextInt(random(), 1, maxNumValues); | ||
for (int j = 0; j < numValuesArray[iter]; j++) { | ||
values[j] = RandomNumbers.randomIntBetween(random(), 0, (int) PackedInts.maxValue(bpv)); | ||
values[j] = | ||
RandomNumbers.randomIntBetween(random(), minValue, (int) PackedInts.maxValue(bpv)); | ||
vIntOut.writeVInt((int) values[j]); | ||
} | ||
groupVIntOut.writeGroupVInts(values, numValuesArray[iter]); | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not mention this implementation detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change has reverted, there is no change in
DataOutput
in current fix approach.