-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix overflow bug in SortingNumericDocValues (#70154)
- Loading branch information
Showing
2 changed files
with
85 additions
and
4 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
71 changes: 71 additions & 0 deletions
71
server/src/test/java/org/elasticsearch/index/fielddata/SortingNumericDocValuesTests.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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.index.fielddata; | ||
|
||
import org.apache.lucene.util.ArrayUtil; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.hamcrest.Matchers; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.function.LongConsumer; | ||
|
||
public class SortingNumericDocValuesTests extends ESTestCase { | ||
|
||
public void testResize() { | ||
final int oldSize = Integer.MAX_VALUE - 200; | ||
final int newSize = Integer.MAX_VALUE - 100; | ||
// This counter should account for the initialization of the array (size == 1) | ||
// and the diff between newSize (over-sized) and oldSize. | ||
final AtomicLong counter = new AtomicLong(); | ||
LongConsumer consumer = value -> { | ||
long total = counter.addAndGet(value); | ||
assertThat(total, Matchers.greaterThanOrEqualTo(0L)); | ||
}; | ||
SortingNumericDocValues docValues = new SortingNumericDocValues(consumer) { | ||
|
||
@Override | ||
protected void growExact(int newValuesLength) { | ||
// don't grow the array | ||
} | ||
|
||
/** Get the size of the internal array using a method so we can override it during testing */ | ||
protected int getArrayLength() { | ||
return oldSize; | ||
} | ||
|
||
@Override | ||
public boolean advanceExact(int target) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public int docID() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int nextDoc() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int advance(int target) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public long cost() { | ||
return 0; | ||
} | ||
}; | ||
docValues.resize(newSize); | ||
final long diff = ArrayUtil.oversize(newSize, Long.BYTES) - oldSize; | ||
assertThat(counter.get(), Matchers.equalTo((diff + 1) * Long.BYTES)); | ||
} | ||
} |