Skip to content

Commit 0882efb

Browse files
Support writing unsigned integers to buffer (#691)
* Support writing unsigned integers to buffer, this is required to support channel ids greater than Integer.MAX_VALUE fixes #690 * Fix incorrect test * Fix indentation to make codacy happy Co-authored-by: Jeroen van Erp <[email protected]>
1 parent b87f21b commit 0882efb

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ public long readUInt32()
306306
data[rpos++] & 0x000000ffL;
307307
}
308308

309+
/**
310+
* Writes a uint32 integer
311+
*
312+
* @param uint32
313+
*
314+
* @return this
315+
*/
316+
@SuppressWarnings("unchecked")
317+
public T putUInt32FromInt(int uint32) {
318+
ensureCapacity(4);
319+
data[wpos++] = (byte) (uint32 >> 24);
320+
data[wpos++] = (byte) (uint32 >> 16);
321+
data[wpos++] = (byte) (uint32 >> 8);
322+
data[wpos++] = (byte) uint32;
323+
return (T) this;
324+
}
325+
309326
/**
310327
* Writes a uint32 integer
311328
*

src/main/java/net/schmizz/sshj/connection/ConnectionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private void gotChannelOpen(SSHPacket buf)
245245
public void sendOpenFailure(int recipient, Reason reason, String message)
246246
throws TransportException {
247247
trans.write(new SSHPacket(Message.CHANNEL_OPEN_FAILURE)
248-
.putUInt32(recipient)
248+
.putUInt32FromInt(recipient)
249249
.putUInt32(reason.getCode())
250250
.putString(message));
251251
}

src/main/java/net/schmizz/sshj/connection/channel/AbstractChannel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ protected void handleRequest(String reqType, SSHPacket buf)
352352
}
353353

354354
protected SSHPacket newBuffer(Message cmd) {
355-
return new SSHPacket(cmd).putUInt32(recipient);
355+
return new SSHPacket(cmd).putUInt32FromInt(recipient);
356356
}
357357

358358
protected void receiveInto(ChannelInputStream stream, SSHPacket buf)

src/main/java/net/schmizz/sshj/connection/channel/ChannelInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private void checkWindow()
151151
if (adjustment > 0) {
152152
log.debug("Sending SSH_MSG_CHANNEL_WINDOW_ADJUST to #{} for {} bytes", chan.getRecipient(), adjustment);
153153
trans.write(new SSHPacket(Message.CHANNEL_WINDOW_ADJUST)
154-
.putUInt32(chan.getRecipient()).putUInt32(adjustment));
154+
.putUInt32FromInt(chan.getRecipient()).putUInt32(adjustment));
155155
win.expand(adjustment);
156156
}
157157
}

src/main/java/net/schmizz/sshj/connection/channel/ChannelOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ boolean flush(int bufferSize, boolean canAwaitExpansion) throws TransportExcepti
9090

9191
packet.wpos(headerOffset);
9292
packet.putMessageID(Message.CHANNEL_DATA);
93-
packet.putUInt32(chan.getRecipient());
93+
packet.putUInt32FromInt(chan.getRecipient());
9494
packet.putUInt32(writeNow);
9595
packet.wpos(dataOffset + writeNow);
9696

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,25 @@
2525

2626
public class BufferTest {
2727

28+
@Test
29+
public void testNegativeInteger() throws BufferException {
30+
byte[] negativeInt = new byte[] { (byte) 0xB8,
31+
(byte) 0x4B,
32+
(byte) 0xF4,
33+
(byte) 0x38 };
34+
PlainBuffer buffer = new PlainBuffer(negativeInt);
35+
assertEquals(buffer.readUInt32AsInt(),-1202981832);
36+
37+
PlainBuffer buff = new PlainBuffer();
38+
buff.ensureCapacity(4);
39+
buff.putUInt32FromInt(-1202981832);
40+
byte[] data = buff.getCompactData();
41+
assertEquals(data[0], (byte) 0xB8);
42+
assertEquals(data[1], (byte) 0x4B);
43+
assertEquals(data[2], (byte) 0xF4);
44+
assertEquals(data[3], (byte) 0x38);
45+
}
46+
2847
// Issue 72: previously, it entered an infinite loop trying to establish the buffer size
2948
@Test
3049
public void shouldThrowOnTooLargeCapacity() {

0 commit comments

Comments
 (0)