Skip to content

Commit 1dc4860

Browse files
committed
prevent BAIS underflow
1 parent 222e820 commit 1dc4860

File tree

1 file changed

+11
-3
lines changed
  • src/main/java/com/trivago/fastutilconcurrentwrapper/io

1 file changed

+11
-3
lines changed

src/main/java/com/trivago/fastutilconcurrentwrapper/io/BAIS.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,27 +161,31 @@ public boolean readBoolean () {
161161
/// (short)((read() << 8)|(read() & 0xFF))
162162
@Override
163163
public short readShort () {
164+
if (count < pos + 2) throw new ArrayIndexOutOfBoundsException("readShort, but "+available());// EOF
164165
short v = JBytes.DirectByteArrayAccess.getShort(buf, pos);
165166
pos += 2;
166167
return v;
167168
}
168169

169170
@Override
170-
public int readUnsignedShort() {
171+
public int readUnsignedShort () {
172+
if (count < pos + 2) throw new ArrayIndexOutOfBoundsException("readUnsignedShort, but "+available());// EOF
171173
int v = JBytes.DirectByteArrayAccess.getUnsignedShort(buf, pos);
172174
pos += 2;
173175
return v;
174176
}
175177

176178
@Override
177-
public char readChar() {
179+
public char readChar () {
180+
if (count < pos + 2) throw new ArrayIndexOutOfBoundsException("readChar, but "+available());// EOF
178181
char v = JBytes.DirectByteArrayAccess.getChar(buf, pos);
179182
pos += 2;
180183
return v;
181184
}
182185

183186
@Override
184-
public int readInt() {
187+
public int readInt () {
188+
if (count < pos + 4) throw new ArrayIndexOutOfBoundsException("readInt, but "+available());// EOF
185189
//return read() << 24 | ((read() & 0xFF) << 16) | ((read() & 0xFF) << 8) | (read() & 0xFF);
186190
int v = JBytes.DirectByteArrayAccess.getInt(buf, pos);
187191
pos += 4;
@@ -195,6 +199,7 @@ public int readMedium () {
195199
/// @see UUID#UUID(long, long)
196200
/// @see UUID#fromString(String)
197201
public UUID readUUID () {
202+
if (count < pos + 16) throw new ArrayIndexOutOfBoundsException("readUUID, but "+available());// EOF
198203
//val bb = ByteBuffer.wrap(bytes); long mostSigBits = bb.getLong(); long leastSigBits = bb.getLong(); быстрее за счёт VarHandle
199204
long mostSigBits = readLong();// 0..7
200205
long leastSigBits = readLong();// 8..15
@@ -203,6 +208,7 @@ public UUID readUUID () {
203208

204209
@Override
205210
public long readLong () {
211+
if (count < pos + 8) throw new ArrayIndexOutOfBoundsException("readLong, but "+available());// EOF
206212
//return (long) readInt() << 32 | (readInt() & 0xFFFF_FFFFL);
207213
long v = JBytes.DirectByteArrayAccess.getLong(buf, pos);
208214
pos += 8;
@@ -211,13 +217,15 @@ public long readLong () {
211217

212218
@Override
213219
public float readFloat () {
220+
if (count < pos + 4) throw new ArrayIndexOutOfBoundsException("readFloat, but "+available());// EOF
214221
float v = JBytes.DirectByteArrayAccess.getFloat(buf, pos);
215222
pos += 4;
216223
return v;
217224
}
218225

219226
@Override
220227
public double readDouble () {
228+
if (count < pos + 8) throw new ArrayIndexOutOfBoundsException("readDouble, but "+available());// EOF
221229
double v = JBytes.DirectByteArrayAccess.getDouble(buf, pos);
222230
pos += 8;
223231
return v;

0 commit comments

Comments
 (0)