-
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 all 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
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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
/* | ||
* 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 TestPostingsUtil extends LuceneTestCase { | ||
|
||
// checks for bug described in https://github.com/apache/lucene/issues/13373 | ||
public void testIntegerOverflow() throws IOException { | ||
final int size = random().nextInt(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
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.
Maybe use
Long.compareUnsigned
? (if (Long.compareUnsigned(value, 0xFFFFFFFFL) > 0)
)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.
Great!