Fix integer overflow issues.

This commit is contained in:
David Renshaw 2015-04-20 21:10:52 -04:00
parent b56b91531c
commit 717d977233
3 changed files with 147 additions and 30 deletions

View file

@ -438,6 +438,123 @@ class EncodingSuite extends FunSuite {
root.getAnyPointerField().getAs(new StructList.Factory(TestAllTypes.factory));
}
test("LongUint8List") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 28) + 1;
val list = allTypes.initUInt8List(length);
list.size() should equal (length);
list.set(length - 1, 3);
list.get(length - 1) should equal (3);
allTypes.asReader().getUInt8List().get(length - 1) should equal (3);
}
}
test("LongUint16List") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 27) + 1;
val list = allTypes.initUInt16List(length);
list.size() should equal (length);
list.set(length - 1, 3);
list.get(length - 1) should equal (3);
allTypes.asReader().getUInt16List().get(length - 1) should equal (3);
}
}
test("LongUint32List") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 26) + 1;
val list = allTypes.initUInt32List(length);
list.size() should equal (length);
list.set(length - 1, 3);
list.get(length - 1) should equal (3);
allTypes.asReader().getUInt32List().get(length - 1) should equal (3);
}
}
test("LongUint64List") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 25) + 1;
val list = allTypes.initUInt64List(length);
list.size() should equal (length);
list.set(length - 1, 3);
list.get(length - 1) should equal (3);
allTypes.asReader().getUInt64List().get(length - 1) should equal (3);
}
}
test("LongFloat32List") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 26) + 1;
val list = allTypes.initFloat32List(length);
list.size() should equal (length);
list.set(length - 1, 3.14f);
list.get(length - 1) should equal (3.14f);
allTypes.asReader().getFloat32List().get(length - 1) should equal (3.14f);
}
}
test("LongFloat64List") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 25) + 1;
val list = allTypes.initFloat64List(length);
list.size() should equal (length);
list.set(length - 1, 3.14);
list.get(length - 1) should equal (3.14);
allTypes.asReader().getFloat64List().get(length - 1) should equal (3.14);
}
}
test("LongStructList") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 21) + 1;
val list = allTypes.initStructList(length);
list.size() should equal (length);
list.get(length - 1).setUInt8Field(3);
allTypes.asReader().getStructList().get(length - 1).getUInt8Field() should equal (3);
}
}
test("LongTextList") {
{
val message = new MessageBuilder();
val allTypes = message.initRoot(TestAllTypes.factory);
val length = (1 << 25) + 1;
val list = allTypes.initTextList(length);
list.size() should equal (length);
list.set(length - 1, new Text.Reader("foo"));
allTypes.asReader().getTextList().get(length - 1).toString() should equal ("foo");
}
}
test("LongListList") {
{
val message = new MessageBuilder();
val root = message.initRoot(TestLists.factory);
val length = (1 << 25) + 1;
val list = root.initStructListList(length);
list.size() should equal (length);
list.init(length - 1, 3);
list.get(length - 1).size() should equal (3);
root.asReader().getStructListList().get(length - 1).size() should equal (3);
}
}
// to debug, do this:
//Serialize.write((new java.io.FileOutputStream("/Users/dwrensha/Desktop/test.dat")).getChannel(),

View file

@ -51,33 +51,33 @@ public class ListBuilder {
}
protected boolean _getBooleanElement(int index) {
long bindex = index * this.step;
long bindex = (long)index * this.step;
byte b = this.segment.buffer.get(this.ptr + (int)(bindex / Constants.BITS_PER_BYTE));
return (b & (1 << (bindex % 8))) != 0;
}
protected byte _getByteElement(int index) {
return this.segment.buffer.get(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.get(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected short _getShortElement(int index) {
return this.segment.buffer.getShort(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getShort(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected int _getIntElement(int index) {
return this.segment.buffer.getInt(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getInt(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected long _getLongElement(int index) {
return this.segment.buffer.getLong(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getLong(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected float _getFloatElement(int index) {
return this.segment.buffer.getFloat(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getFloat(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected double _getDoubleElement(int index) {
return this.segment.buffer.getDouble(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getDouble(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected void _setBooleanElement(int index, boolean value) {
@ -90,32 +90,32 @@ public class ListBuilder {
}
protected void _setByteElement(int index, byte value) {
this.segment.buffer.put(this.ptr + index * this.step / Constants.BITS_PER_BYTE, value);
this.segment.buffer.put(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE), value);
}
protected void _setShortElement(int index, short value) {
this.segment.buffer.putShort(this.ptr + index * this.step / Constants.BITS_PER_BYTE, value);
this.segment.buffer.putShort(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE), value);
}
protected void _setIntElement(int index, int value) {
this.segment.buffer.putInt(this.ptr + index * this.step / Constants.BITS_PER_BYTE, value);
this.segment.buffer.putInt(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE), value);
}
protected void _setLongElement(int index, long value) {
this.segment.buffer.putLong(this.ptr + index * this.step / Constants.BITS_PER_BYTE, value);
this.segment.buffer.putLong(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE), value);
}
protected void _setFloatElement(int index, float value) {
this.segment.buffer.putFloat(this.ptr + index * this.step / Constants.BITS_PER_BYTE, value);
this.segment.buffer.putFloat(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE), value);
}
protected void _setDoubleElement(int index, double value) {
this.segment.buffer.putDouble(this.ptr + index * this.step / Constants.BITS_PER_BYTE, value);
this.segment.buffer.putDouble(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE), value);
}
protected final <T> T _getStructElement(StructBuilder.Factory<T> factory, int index) {
int indexBit = index * this.step;
int structData = this.ptr + indexBit / 8 ;
long indexBit = (long) index * this.step;
int structData = this.ptr + (int)(indexBit / Constants.BITS_PER_BYTE);
int structPointers = (structData + (this.structDataSize / 8)) / 8;
return factory.constructBuilder(this.segment,
@ -128,19 +128,19 @@ public class ListBuilder {
protected final <T> T _getPointerElement(FromPointerBuilder<T> factory, int index) {
return factory.fromPointerBuilder(
this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD);
(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD);
}
protected final <T> T _initPointerElement(FromPointerBuilder<T> factory, int index, int elementCount) {
return factory.initFromPointerBuilder(
this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
elementCount);
}
protected final <Builder, Reader> void _setPointerElement(SetPointerBuilder<Builder, Reader> factory, int index, Reader value) {
factory.setPointerBuilder(this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
value);
}

View file

@ -67,41 +67,41 @@ public class ListReader {
}
protected boolean _getBooleanElement(int index) {
long bindex = index * this.step;
long bindex = (long)index * this.step;
byte b = this.segment.buffer.get(this.ptr + (int)(bindex / Constants.BITS_PER_BYTE));
return (b & (1 << (bindex % 8))) != 0;
}
protected byte _getByteElement(int index) {
return this.segment.buffer.get(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.get(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected short _getShortElement(int index) {
return this.segment.buffer.getShort(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getShort(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected int _getIntElement(int index) {
return this.segment.buffer.getInt(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getInt(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected long _getLongElement(int index) {
return this.segment.buffer.getLong(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getLong(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected float _getFloatElement(int index) {
return this.segment.buffer.getFloat(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getFloat(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected double _getDoubleElement(int index) {
return this.segment.buffer.getDouble(this.ptr + index * this.step / Constants.BITS_PER_BYTE);
return this.segment.buffer.getDouble(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE));
}
protected <T> T _getStructElement(StructReader.Factory<T> factory, int index) {
// TODO check nesting limit
int indexBit = index * this.step;
int structData = this.ptr + (indexBit / 8);
int structPointers = structData + (this.structDataSize / 8);
long indexBit = (long)index * this.step;
int structData = this.ptr + (int)(indexBit / Constants.BITS_PER_BYTE);
int structPointers = structData + (this.structDataSize / Constants.BITS_PER_BYTE);
return factory.constructReader(this.segment, structData, structPointers / 8, this.structDataSize,
this.structPointerCount, this.nestingLimit - 1);
@ -109,7 +109,7 @@ public class ListReader {
protected <T> T _getPointerElement(FromPointerReader<T> factory, int index) {
return factory.fromPointerReader(this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
this.nestingLimit);
}
@ -117,7 +117,7 @@ public class ListReader {
java.nio.ByteBuffer defaultBuffer, int defaultOffset, int defaultSize) {
return factory.fromPointerReaderBlobDefault(
this.segment,
(this.ptr + (index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
(this.ptr + (int)((long)index * this.step / Constants.BITS_PER_BYTE)) / Constants.BYTES_PER_WORD,
defaultBuffer,
defaultOffset,
defaultSize);