Browse Source

Improve bounds checking.

master
Andy Nguyen 1 year ago
parent
commit
3347cbb544
  1. 20
      src/com/bdjb/api/Buffer.java
  2. 2
      src/com/bdjb/api/IntBase.java

20
src/com/bdjb/api/Buffer.java

@ -40,42 +40,42 @@ public class Buffer { @@ -40,42 +40,42 @@ public class Buffer {
}
public byte getByte(int offset) {
checkOffset(offset);
checkOffset(offset, Int8.SIZE);
return api.read8(address + offset);
}
public short getShort(int offset) {
checkOffset(offset);
checkOffset(offset, Int16.SIZE);
return api.read16(address + offset);
}
public int getInt(int offset) {
checkOffset(offset);
checkOffset(offset, Int32.SIZE);
return api.read32(address + offset);
}
public long getLong(int offset) {
checkOffset(offset);
checkOffset(offset, Int64.SIZE);
return api.read64(address + offset);
}
public void putByte(int offset, byte value) {
checkOffset(offset);
checkOffset(offset, Int8.SIZE);
api.write8(address + offset, value);
}
public void putShort(int offset, short value) {
checkOffset(offset);
checkOffset(offset, Int16.SIZE);
api.write16(address + offset, value);
}
public void putInt(int offset, int value) {
checkOffset(offset);
checkOffset(offset, Int32.SIZE);
api.write32(address + offset, value);
}
public void putLong(int offset, long value) {
checkOffset(offset);
checkOffset(offset, Int64.SIZE);
api.write64(address + offset, value);
}
@ -83,8 +83,8 @@ public class Buffer { @@ -83,8 +83,8 @@ public class Buffer {
api.memset(address, value, size);
}
private void checkOffset(int offset) {
if (offset < 0 || offset >= size) {
private void checkOffset(int offset, int length) {
if (offset < 0 || (offset + length) > size) {
throw new IndexOutOfBoundsException();
}
}

2
src/com/bdjb/api/IntBase.java

@ -72,7 +72,7 @@ abstract class IntBase { @@ -72,7 +72,7 @@ abstract class IntBase {
}
private void checkOffset(int offset) {
if (offset < 0 || offset >= size) {
if (offset < 0 || (offset + elementSize()) > size) {
throw new IndexOutOfBoundsException();
}
}

Loading…
Cancel
Save