Skip to content

Commit

Permalink
Support writing unsigned integers to buffer (#691)
Browse files Browse the repository at this point in the history
* 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 <jeroen@hierynomus.com>
  • Loading branch information
matenhagen and hierynomus authored May 26, 2021
1 parent b87f21b commit 0882efb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/main/java/net/schmizz/sshj/common/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ public long readUInt32()
data[rpos++] & 0x000000ffL;
}

/**
* Writes a uint32 integer
*
* @param uint32
*
* @return this
*/
@SuppressWarnings("unchecked")
public T putUInt32FromInt(int uint32) {
ensureCapacity(4);
data[wpos++] = (byte) (uint32 >> 24);
data[wpos++] = (byte) (uint32 >> 16);
data[wpos++] = (byte) (uint32 >> 8);
data[wpos++] = (byte) uint32;
return (T) this;
}

/**
* Writes a uint32 integer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private void gotChannelOpen(SSHPacket buf)
public void sendOpenFailure(int recipient, Reason reason, String message)
throws TransportException {
trans.write(new SSHPacket(Message.CHANNEL_OPEN_FAILURE)
.putUInt32(recipient)
.putUInt32FromInt(recipient)
.putUInt32(reason.getCode())
.putString(message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ protected void handleRequest(String reqType, SSHPacket buf)
}

protected SSHPacket newBuffer(Message cmd) {
return new SSHPacket(cmd).putUInt32(recipient);
return new SSHPacket(cmd).putUInt32FromInt(recipient);
}

protected void receiveInto(ChannelInputStream stream, SSHPacket buf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void checkWindow()
if (adjustment > 0) {
log.debug("Sending SSH_MSG_CHANNEL_WINDOW_ADJUST to #{} for {} bytes", chan.getRecipient(), adjustment);
trans.write(new SSHPacket(Message.CHANNEL_WINDOW_ADJUST)
.putUInt32(chan.getRecipient()).putUInt32(adjustment));
.putUInt32FromInt(chan.getRecipient()).putUInt32(adjustment));
win.expand(adjustment);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ boolean flush(int bufferSize, boolean canAwaitExpansion) throws TransportExcepti

packet.wpos(headerOffset);
packet.putMessageID(Message.CHANNEL_DATA);
packet.putUInt32(chan.getRecipient());
packet.putUInt32FromInt(chan.getRecipient());
packet.putUInt32(writeNow);
packet.wpos(dataOffset + writeNow);

Expand Down
19 changes: 19 additions & 0 deletions src/test/java/net/schmizz/sshj/common/BufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@

public class BufferTest {

@Test
public void testNegativeInteger() throws BufferException {
byte[] negativeInt = new byte[] { (byte) 0xB8,
(byte) 0x4B,
(byte) 0xF4,
(byte) 0x38 };
PlainBuffer buffer = new PlainBuffer(negativeInt);
assertEquals(buffer.readUInt32AsInt(),-1202981832);

PlainBuffer buff = new PlainBuffer();
buff.ensureCapacity(4);
buff.putUInt32FromInt(-1202981832);
byte[] data = buff.getCompactData();
assertEquals(data[0], (byte) 0xB8);
assertEquals(data[1], (byte) 0x4B);
assertEquals(data[2], (byte) 0xF4);
assertEquals(data[3], (byte) 0x38);
}

// Issue 72: previously, it entered an infinite loop trying to establish the buffer size
@Test
public void shouldThrowOnTooLargeCapacity() {
Expand Down

0 comments on commit 0882efb

Please sign in to comment.