提交 55e77bde authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 610: possible integer overflow in WriteBuffer.grow().

上级 8f89554c
...@@ -297,13 +297,18 @@ public class WriteBuffer { ...@@ -297,13 +297,18 @@ public class WriteBuffer {
return buff; return buff;
} }
private void grow(int len) { private void grow(int additional) {
ByteBuffer temp = buff; ByteBuffer temp = buff;
int needed = len - temp.remaining(); int needed = additional - temp.remaining();
int grow = Math.max(needed, MIN_GROW); // grow at least MIN_GROW
long grow = Math.max(needed, MIN_GROW);
// grow at least 50% of the current size // grow at least 50% of the current size
grow = Math.max(temp.capacity() / 2, grow); grow = Math.max(temp.capacity() / 2, grow);
int newCapacity = temp.capacity() + grow; // the new capacity is at least Integer.MAX_VALUE
int newCapacity = (int) Math.min(Integer.MAX_VALUE, temp.capacity() + grow);
if (newCapacity < needed) {
throw new OutOfMemoryError("Capacity: " + newCapacity + " needed: " + needed);
}
try { try {
buff = ByteBuffer.allocate(newCapacity); buff = ByteBuffer.allocate(newCapacity);
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论