LUCENE-10187: Reduce DirectWriter's padding. (#398)

It would make us more likely to detect out-of-bounds access in the future.
This commit is contained in:
Adrien Grand 2021-10-20 10:30:09 +02:00 committed by GitHub
parent 54418cef45
commit f13a400b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 2 deletions

View File

@ -149,8 +149,25 @@ public final class DirectWriter {
} }
assert !finished; assert !finished;
flush(); flush();
// pad for fast io: we actually only need this for certain BPV, but its just 3 bytes... // add padding bytes for fast io
for (int i = 0; i < 3; i++) { // for every number of bits per value, we want to be able to read the entire value in a single
// read e.g. for 20 bits per value, we want to be able to read values using ints so we need
// 32 - 20 = 12 bits of padding
int paddingBitsNeeded;
if (bitsPerValue > Integer.SIZE) {
paddingBitsNeeded = Long.SIZE - bitsPerValue;
} else if (bitsPerValue > Short.SIZE) {
paddingBitsNeeded = Integer.SIZE - bitsPerValue;
} else if (bitsPerValue > Byte.SIZE) {
paddingBitsNeeded = Short.SIZE - bitsPerValue;
} else {
paddingBitsNeeded = 0;
}
assert paddingBitsNeeded >= 0;
final int paddingBytesNeeded = (paddingBitsNeeded + Byte.SIZE - 1) / Byte.SIZE;
assert paddingBytesNeeded <= 3;
for (int i = 0; i < paddingBytesNeeded; i++) {
output.writeByte((byte) 0); output.writeByte((byte) 0);
} }
finished = true; finished = true;