Skip to content

Commit 1857b63

Browse files
committed
Fix use after free problem
1 parent 87b7af4 commit 1857b63

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.core.Releasable;
1414
import org.elasticsearch.core.Releasables;
1515
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
16+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramCircuitBreaker;
1617

1718
import java.util.ArrayList;
1819
import java.util.Arrays;
@@ -302,7 +303,11 @@ yield new AggregateMetricDoubleLiteral(
302303
aggBlock.countBlock().getInt(offset)
303304
);
304305
}
305-
case EXPONENTIAL_HISTOGRAM -> ((ExponentialHistogramArrayBlock) block).getExponentialHistogram(offset);
306+
case EXPONENTIAL_HISTOGRAM -> ExponentialHistogram.merge( //TODO: do a cleaner copy with builder here
307+
100,
308+
ExponentialHistogramCircuitBreaker.noop(),
309+
((ExponentialHistogramArrayBlock) block).getExponentialHistogram(offset)
310+
);
306311
case UNKNOWN -> throw new IllegalArgumentException("can't read values from [" + block + "]");
307312
};
308313
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ExponentialHistogramArrayBlock.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public final class ExponentialHistogramArrayBlock extends AbstractNonThreadSafeR
2525

2626
// TODO: Add AggregateMetricDoubleBlock for min/max/sum/count
2727
private final BytesRefBlock encodedHistograms;
28+
private boolean isClosed = false;
2829

2930
public ExponentialHistogramArrayBlock(BytesRefBlock encodedHistograms) {
3031
this.encodedHistograms = encodedHistograms;
@@ -42,6 +43,7 @@ static BytesRef encode(ExponentialHistogram histogram, BytesRef growableScratch)
4243

4344
@Override
4445
protected void closeInternal() {
46+
isClosed = true;
4547
Releasables.close(encodedHistograms);
4648
}
4749

@@ -207,9 +209,16 @@ private static void resizeIfRequired(BytesRef growableBuffer, int totalSize) {
207209

208210
@Override
209211
public int scale() {
212+
checkForUseAfterClose();
210213
return data.get(data.position() + SCALE_OFFSET);
211214
}
212215

216+
private void checkForUseAfterClose() {
217+
if (ExponentialHistogramArrayBlock.this.isClosed) {
218+
throw new IllegalStateException("Backing block has already been closed!");
219+
}
220+
}
221+
213222
@Override
214223
public ZeroBucket zeroBucket() {
215224
// TODO: implement

0 commit comments

Comments
 (0)