[jOOQ/jOOQ#18449] IN list padding generates too large lists in rare edge cases due to floating point rounding errors

This commit is contained in:
Lukas Eder 2025-05-14 15:31:51 +02:00
parent c85c24f93e
commit 24b54a5b11

View File

@ -300,7 +300,24 @@ abstract class AbstractInList<T> extends AbstractCondition {
this.delegate = delegate;
this.realSize = delegate.size();
this.padSize = Math.min(maxPadding, (int) Math.round(Math.pow(b, Math.ceil(Math.log(realSize) / Math.log(b)))));
this.padSize = Math.min(maxPadding, padSize(Math.min(maxPadding, realSize), b));
}
static final int padSize(int max, int b) {
int n, r = 1;
// [#18449] Use a loop instead of the previous, simpler log arithmetic to avoid floating point rounding issues:
// Math.min(maxPadding, (int) Math.round(Math.pow(b, Math.ceil(Math.log(realSize) / Math.log(b)))));
// We'll iterate at most 31 times for huge lists and base 2.
// n > 0 is to handle the unlikely event of an overflow.
while ((n = r * b) < max && n > 0)
r = n;
return n < 0
? Integer.MAX_VALUE
: r == max
? max
: n;
}
@Override