Skip to content
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 overflow bug in SortingNumericDocValues #70154

Merged
merged 4 commits into from
Mar 10, 2021

Conversation

iverase
Copy link
Contributor

@iverase iverase commented Mar 9, 2021

This was found when performing some abusing geogrid aggregations. During resizing of the array, we are computing the size of the array as an integer, which may overflow if the size of the new array is big. This PR just make sure that computing the new size is done using long.

@iverase iverase added >bug :Analytics/Geo Indexing, search aggregations of geo points and shapes v8.0.0 v7.13.0 labels Mar 9, 2021
@iverase iverase requested a review from nik9000 March 9, 2021 14:44
@elasticmachine elasticmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Mar 9, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-analytics-geo (Team:Analytics)

@Override
public void accept(long value) {
total += value;
assertThat(total, Matchers.greaterThanOrEqualTo(0L));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be ever so slightly more readable to make the LongConsumer something like AtomicLong::getAndAdd and then assert the size is exactly what you expect at the bottom. I guess it doesn't assert that you don't have transient negative values though. Maybe do both? Not sure. This is fine too, really.

@@ -70,15 +70,20 @@ protected final void resize(int newSize) {
// copying.
long oldValuesSizeInBytes = values.length * Long.BYTES;
int newValuesLength = ArrayUtil.oversize(newSize, Long.BYTES);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the expression above:

long oldValuesSizeInBytes = values.length * Long.BYTES;

Can overflow as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because length and Long.BYTES are both int, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

booooo. And you can't test that one easily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that is tricky to test now. we can make getLength a protected method that can be override?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I guess so. I think because this thing is designed for extension maybe we should do something else? These two new methods are only for testing which is fine if you are compositing this thing. But when you extend it its hard to know which to extend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should be just add asserts in the code. I think this is what they are design for?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the overrides you can't put the thing into a state where we'd see the overflows. I'd just go with the protected method and a comment, I think. I just kind of got confused.

@iverase
Copy link
Contributor Author

iverase commented Mar 9, 2021

@nik9000 another iteration, it leaks a bit the internal implementation but we are testing that anyway.

Copy link
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. It'll be a little more confusing to extend but I imagine anyone extending it will pretty quickly see what is going on.

@iverase iverase merged commit c3f11f2 into elastic:master Mar 10, 2021
@iverase iverase deleted the SortingNumericDocValues branch March 10, 2021 06:32
iverase added a commit to iverase/elasticsearch that referenced this pull request Mar 10, 2021
easyice pushed a commit to easyice/elasticsearch that referenced this pull request Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/Geo Indexing, search aggregations of geo points and shapes >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v7.12.1 v7.13.0 v8.0.0-alpha1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants