Prevent integer overflow calculating QueryQueue permits
Detect and report integer overflow when QueryQueue's permits cannot be
calculated. Not using Math.addExact() to be able to report problem
nicely.
@@ -38,7 +38,11 @@ public class QueryQueue
checkArgument(maxQueuedQueries > 0, "maxQueuedQueries must be greater than zero");
checkArgument(maxConcurrentQueries > 0, "maxConcurrentQueries must be greater than zero");
- this.queuePermits = new AtomicInteger(maxQueuedQueries + maxConcurrentQueries);
+ int permits = maxQueuedQueries + maxConcurrentQueries;
+ // Check for overflow
+ checkArgument(permits > 0, "maxQueuedQueries + maxConcurrentQueries must be less than or equal to %s", Integer.MAX_VALUE);
+
+ this.queuePermits = new AtomicInteger(permits);
this.asyncSemaphore = new AsyncSemaphore<>(maxConcurrentQueries,
queryExecutor,
queueEntry -> {
