Skip to content

Commit 17c368f

Browse files
OlivierSalaschierynomus
authored andcommitted
add Buffer capacity check for type UInt64 (#454)
1 parent 4de9f8a commit 17c368f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/main/java/net/schmizz/sshj/common/Buffer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ public T putUInt64(BigInteger uint64) {
372372

373373
@SuppressWarnings("unchecked")
374374
private T putUInt64Unchecked(long uint64) {
375+
ensureCapacity(8);
375376
data[wpos++] = (byte) (uint64 >> 56);
376377
data[wpos++] = (byte) (uint64 >> 48);
377378
data[wpos++] = (byte) (uint64 >> 40);

src/test/java/net/schmizz/sshj/common/BufferTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,28 @@ public void shouldHaveSameUInt64EncodingForBigIntegerAndLong() {
146146
assertArrayEquals("Value: " + value, bytesLong, bytesBigInt);
147147
}
148148
}
149+
150+
151+
@Test
152+
public void shouldExpandCapacityOfUInt32(){
153+
PlainBuffer buf = new PlainBuffer();
154+
for(int i=0;i<Buffer.DEFAULT_SIZE+1;i+=4) {
155+
buf.putUInt32(1l);
156+
}
157+
/* Buffer should have been expanded at this point*/
158+
assertEquals(Buffer.DEFAULT_SIZE*2,buf.data.length);
159+
}
160+
161+
@Test
162+
public void shouldExpandCapacityOfUInt64(){
163+
BigInteger bigUint64 = BigInteger.valueOf(Long.MAX_VALUE);
164+
PlainBuffer buf = new PlainBuffer();
165+
assertEquals(Buffer.DEFAULT_SIZE,buf.data.length);
166+
for(int i=0;i<Buffer.DEFAULT_SIZE+1;i+=8) {
167+
buf.putUInt64(bigUint64.longValue());
168+
}
169+
/* Buffer should have been expanded at this point*/
170+
assertEquals(Buffer.DEFAULT_SIZE*2,buf.data.length);
171+
}
172+
149173
}

0 commit comments

Comments
 (0)