提交 7d8aee68 authored 作者: LaughingMan's avatar LaughingMan

Throw an exception if MathUtils.nextPowerOf2 receives an argument outside of

the expected range. Also handle the exception at all call sites.
上级 034631d5
......@@ -338,8 +338,15 @@ public class SysProperties {
* The maximum number of objects in the cache.
* This value must be a power of 2.
*/
public static final int OBJECT_CACHE_SIZE =
MathUtils.nextPowerOf2(Utils.getProperty("h2.objectCacheSize", 1024));
public static final int OBJECT_CACHE_SIZE;
static {
try {
OBJECT_CACHE_SIZE = MathUtils.nextPowerOf2(
Utils.getProperty("h2.objectCacheSize", 1024));
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Invalid h2.objectCacheSize", e);
}
}
/**
* System property <code>h2.oldStyleOuterJoin</code>
......
......@@ -51,7 +51,12 @@ public class CacheLRU implements Cache {
this.writer = writer;
this.fifo = fifo;
this.setMaxMemory(maxMemoryKb);
long tmpLen = MathUtils.nextPowerOf2(maxMemory / 64);
long tmpLen;
try {
tmpLen = MathUtils.nextPowerOf2(maxMemory / 64);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("This much cache memory is not supported: " + maxMemoryKb + "kb", e);
}
if (tmpLen > Integer.MAX_VALUE) {
throw new IllegalStateException("do not support this much cache memory: " + maxMemoryKb + "kb");
}
......
......@@ -218,10 +218,14 @@ public class MathUtils {
*
* @param x the original value
* @return the next power of two value
* @throws IllegalArgumentException if x < 0 or x > 0x40000000
*/
public static int nextPowerOf2(int x) {
public static int nextPowerOf2(int x) throws IllegalArgumentException {
if (x == 0) {
return 1;
} else if (x < 0 || x > 0x40000000 ) {
throw new IllegalArgumentException("Argument out of range"
+ " [0x0-0x40000000]. Argument was: " + x);
}
x--;
x |= x >> 1;
......@@ -238,10 +242,14 @@ public class MathUtils {
*
* @param x the original value
* @return the next power of two value
* @throws IllegalArgumentException if x < 0 or x > 0x4000000000000000
*/
public static long nextPowerOf2(long x) {
public static long nextPowerOf2(long x) throws IllegalArgumentException {
if (x == 0) {
return 1;
} else if (x < 0 || x > 0x4000000000000000L ) {
throw new IllegalArgumentException("Argument out of range"
+ " [0x0-0x4000000000000000]. Argument was: " + x);
}
x--;
x |= x >> 1;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论