Skip to content

Conversation

@Kguswo
Copy link

@Kguswo Kguswo commented Aug 5, 2025

Change Summary

Fix NullPointerException in LongComparator when search_after contains null values by implementing validation at multiple layers.

As null values in search_after arrays are not valid and cause NPE when passed to Lucene's comparators, this change updates the SearchAfterBuilder to reject null values during JSON parsing, object setting, and FieldDoc creation with clear error messages.

Open to suggestions or corrections!

Related Issue

#132370

@elasticsearchmachine elasticsearchmachine added v9.2.0 needs:triage Requires assignment of a team area label external-contributor Pull request authored by a developer outside the Elasticsearch team labels Aug 5, 2025
@john-wagster john-wagster added the :Search Foundations/Search Catch all for Search Foundations label Aug 14, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch label Aug 14, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-search-foundations (Team:Search Foundations)

@elasticsearchmachine elasticsearchmachine removed the needs:triage Requires assignment of a team area label label Aug 14, 2025
@smalyshev smalyshev added the >bug label Aug 14, 2025
@smalyshev
Copy link
Contributor

@elasticsearchmachine test this please

Copy link
Member

@javanna javanna left a comment

Choose a reason for hiding this comment

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

I left a couple small comments. This looks good to me though, thanks a lot for your contribution!

fieldValues[i] = convertValueFromSortField(values[i], sortField, format);
} else {
fieldValues[i] = null;
throw new IllegalArgumentException("Values cannot contain null at position " + i + ".");
Copy link
Member

Choose a reason for hiding this comment

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

do you know what scenario this covers for in practice?

Copy link
Author

Choose a reason for hiding this comment

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

In the constructor SearchAfterBuilder(StreamInput in), readGenericValue() can return null (case -1), and these null values are directly assigned to the sortValues array, bypassing the validation in setSortValues().

Copy link
Member

@javanna javanna Aug 18, 2025

Choose a reason for hiding this comment

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

Right, I see, but these you can only get from a serialized SearchAfterBuilder, so perhaps null values can only come in from previous versions (before your change is made)? At the same time version based logic complicates things so maybe it is ok to leave it as-is.

Copy link
Author

Choose a reason for hiding this comment

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

I agree with your point about backward compatibility. Keeping the validation as it is seems like the simplest and safest approach. Appreciate the feedback!

values.add(parser.booleanValue());
} else if (token == XContentParser.Token.VALUE_NULL) {
values.add(null);
throw new ParsingException(parser.getTokenLocation(), "Values cannot contain null.");
Copy link
Member

Choose a reason for hiding this comment

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

Should we be more specific in the error message that it's the search_after values?

Copy link
Author

Choose a reason for hiding this comment

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

You're right, that would be much clearer. I think we could update this to:

  • Values cannot contain null.[search_after] values cannot contain null.

Copy link
Member

Choose a reason for hiding this comment

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

++

@javanna javanna self-assigned this Aug 15, 2025
@javanna
Copy link
Member

javanna commented Aug 15, 2025

test this please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

>bug external-contributor Pull request authored by a developer outside the Elasticsearch team :Search Foundations/Search Catch all for Search Foundations Team:Search Foundations Meta label for the Search Foundations team in Elasticsearch v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants