Skip to content

Commit

Permalink
HSEARCH-3352 Fix potential integer overflow for timeouts in the Elast…
Browse files Browse the repository at this point in the history
…icsearch backend
  • Loading branch information
yrodiere committed Dec 13, 2019
1 parent a8b0acf commit 1381faf
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 20 deletions.
Expand Up @@ -132,7 +132,7 @@ public void onFailure(Exception exception) {
}
);

Integer currentTimeoutValue = ( elasticsearchRequest.getTimeoutValue() == null ) ?
long currentTimeoutValue = ( elasticsearchRequest.getTimeoutValue() == null ) ?
globalTimeoutValue : elasticsearchRequest.getTimeoutValue();
TimeUnit currentTimeoutUnit = ( elasticsearchRequest.getTimeoutUnit() == null ) ?
globalTimeoutUnit : elasticsearchRequest.getTimeoutUnit();
Expand Down
Expand Up @@ -50,7 +50,7 @@ public static Builder builder(String method) {
private final String path;
private final Map<String, String> parameters;
private final List<JsonObject> bodyParts;
private final Integer timeoutValue;
private final Long timeoutValue;
private final TimeUnit timeoutUnit;

private ElasticsearchRequest(Builder builder) {
Expand Down Expand Up @@ -78,7 +78,7 @@ public List<JsonObject> getBodyParts() {
return bodyParts;
}

public Integer getTimeoutValue() {
public Long getTimeoutValue() {
return timeoutValue;
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public static final class Builder {

private Map<String, String> parameters;
private List<JsonObject> bodyParts;
private Integer timeoutValue;
private Long timeoutValue;
private TimeUnit timeoutUnit;

private Builder(String method) {
Expand Down Expand Up @@ -172,7 +172,7 @@ public Builder body(JsonObject object) {
return this;
}

public Builder timeout(Integer timeoutValue, TimeUnit timeoutUnit) {
public Builder timeout(Long timeoutValue, TimeUnit timeoutUnit) {
this.timeoutValue = timeoutValue;
this.timeoutUnit = timeoutUnit;
return this;
Expand Down
Expand Up @@ -64,7 +64,7 @@ public class ElasticsearchSearchQueryBuilder<H>
private JsonArray jsonSort;
private Map<DistanceSortKey, Integer> distanceSorts;
private Map<AggregationKey<?>, ElasticsearchSearchAggregation<?>> aggregations;
private Integer timeoutValue;
private Long timeoutValue;
private TimeUnit timeoutUnit;
private boolean exceptionOnTimeout;
private ElasticsearchSearchRequestTransformer requestTransformer;
Expand Down Expand Up @@ -105,15 +105,15 @@ public void addRoutingKey(String routingKey) {
@Override
public void truncateAfter(long timeout, TimeUnit timeUnit) {
// This will override any failAfter. Eventually we could allow the user to set both.
this.timeoutValue = Math.toIntExact( timeout );
this.timeoutValue = timeout;
this.timeoutUnit = timeUnit;
this.exceptionOnTimeout = false;
}

@Override
public void failAfter(long timeout, TimeUnit timeUnit) {
// This will override any truncateAfter. Eventually we could allow the user to set both.
this.timeoutValue = Math.toIntExact( timeout );
this.timeoutValue = timeout;
this.timeoutUnit = timeUnit;
this.exceptionOnTimeout = true;
}
Expand Down
Expand Up @@ -58,7 +58,7 @@ public class ElasticsearchSearchQueryImpl<H> extends AbstractSearchQuery<H, Elas
private final ElasticsearchSearchRequestTransformer requestTransformer;
private final ElasticsearchSearchResultExtractor<ElasticsearchLoadableSearchResult<H>> searchResultExtractor;

private Integer timeoutValue;
private Long timeoutValue;
private TimeUnit timeoutUnit;
private boolean exceptionOnTimeout;

Expand All @@ -71,7 +71,7 @@ public class ElasticsearchSearchQueryImpl<H> extends AbstractSearchQuery<H, Elas
JsonObject payload,
ElasticsearchSearchRequestTransformer requestTransformer,
ElasticsearchSearchResultExtractor<ElasticsearchLoadableSearchResult<H>> searchResultExtractor,
Integer timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout) {
Long timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout) {
this.workFactory = workFactory;
this.queryOrchestrator = queryOrchestrator;
this.searchContext = searchContext;
Expand Down Expand Up @@ -216,7 +216,7 @@ private JsonObject doExplain(URLEncodedString encodedIndexName, String id) {

@Override
public void failAfter(long timeout, TimeUnit timeUnit) {
timeoutValue = Math.toIntExact( timeout );
timeoutValue = timeout;
timeoutUnit = timeUnit;
exceptionOnTimeout = true;
}
Expand Down
Expand Up @@ -23,6 +23,6 @@ public interface CountWorkBuilder extends ElasticsearchWorkBuilder<Elasticsearch

CountWorkBuilder requestTransformer(Function<ElasticsearchRequest, ElasticsearchRequest> requestTransformer);

CountWorkBuilder timeout(Integer timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout);
CountWorkBuilder timeout(Long timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout);

}
Expand Up @@ -28,5 +28,5 @@ public interface SearchWorkBuilder<R> extends ElasticsearchWorkBuilder<Elasticse

SearchWorkBuilder<R> requestTransformer(Function<ElasticsearchRequest, ElasticsearchRequest> requestTransformer);

SearchWorkBuilder<R> timeout(Integer timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout);
SearchWorkBuilder<R> timeout(Long timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout);
}
Expand Up @@ -161,7 +161,7 @@ public B requestTransformer(Function<ElasticsearchRequest, ElasticsearchRequest>
return (B) this;
}

protected static String getTimeoutString(Integer timeoutValue, TimeUnit timeoutUnit) {
protected static String getTimeoutString(Long timeoutValue, TimeUnit timeoutUnit) {
StringBuilder builder = new StringBuilder( timeoutValue.toString() );
switch ( timeoutUnit ) {
case DAYS:
Expand Down
Expand Up @@ -40,7 +40,7 @@ public static class Builder extends AbstractBuilder<Builder> implements CountWor
private final List<URLEncodedString> indexNames = new ArrayList<>();
private JsonObject query;
private Set<String> routingKeys;
private Integer timeoutValue;
private Long timeoutValue;
private TimeUnit timeoutUnit;
private boolean exceptionOnTimeout;

Expand All @@ -62,7 +62,7 @@ public Builder routingKeys(Set<String> routingKeys) {
}

@Override
public CountWorkBuilder timeout(Integer timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout) {
public CountWorkBuilder timeout(Long timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout) {
this.timeoutValue = timeoutValue;
this.timeoutUnit = timeoutUnit;
this.exceptionOnTimeout = exceptionOnTimeout;
Expand Down
Expand Up @@ -75,7 +75,7 @@ public static <T> Builder<T> forElasticsearch7AndAbove(JsonObject payload, Elast
private Integer scrollSize;
private String scrollTimeout;
private Set<String> routingKeys;
private Integer timeoutValue;
private Long timeoutValue;
private TimeUnit timeoutUnit;
private boolean exceptionOnTimeout;

Expand Down Expand Up @@ -113,7 +113,7 @@ public SearchWorkBuilder<R> routingKeys(Set<String> routingKeys) {
}

@Override
public SearchWorkBuilder<R> timeout(Integer timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout) {
public SearchWorkBuilder<R> timeout(Long timeoutValue, TimeUnit timeoutUnit, boolean exceptionOnTimeout) {
this.timeoutValue = timeoutValue;
this.timeoutUnit = timeoutUnit;
this.exceptionOnTimeout = exceptionOnTimeout;
Expand Down
Expand Up @@ -189,9 +189,9 @@ private UnsupportedOperationException resultStreamingNotImplemented() {

@Override
public HibernateOrmSearchQueryAdapter<R> setHint(String hintName, Object value) {
if ( "javax.persistence.query.timeout".equals( hintName ) && value instanceof Integer &&
if ( "javax.persistence.query.timeout".equals( hintName ) && value instanceof Long &&
delegate instanceof SearchQueryImplementor ) {
( (SearchQueryImplementor) delegate ).failAfter( (Integer) value, TimeUnit.MILLISECONDS );
( (SearchQueryImplementor) delegate ).failAfter( (Long) value, TimeUnit.MILLISECONDS );
}
return this;
}
Expand Down

0 comments on commit 1381faf

Please sign in to comment.