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 hierynomus/sshj#690

* Fix incorrect test

* Fix indentation to make codacy happy

Co-authored-by: Jeroen van Erp <jeroen@hierynomus.com>
This commit is contained in:
Michiel ten Hagen
2021-05-26 12:34:55 +02:00
committed by GitHub
parent b87f21b7f9
commit 0882efb5cb
6 changed files with 40 additions and 4 deletions

View File

@@ -306,6 +306,23 @@ public class Buffer<T extends Buffer<T>> {
data[rpos++] & 0x000000ffL; 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 * Writes a uint32 integer
* *

View File

@@ -245,7 +245,7 @@ public class ConnectionImpl
public void sendOpenFailure(int recipient, Reason reason, String message) public void sendOpenFailure(int recipient, Reason reason, String message)
throws TransportException { throws TransportException {
trans.write(new SSHPacket(Message.CHANNEL_OPEN_FAILURE) trans.write(new SSHPacket(Message.CHANNEL_OPEN_FAILURE)
.putUInt32(recipient) .putUInt32FromInt(recipient)
.putUInt32(reason.getCode()) .putUInt32(reason.getCode())
.putString(message)); .putString(message));
} }

View File

@@ -352,7 +352,7 @@ public abstract class AbstractChannel
} }
protected SSHPacket newBuffer(Message cmd) { protected SSHPacket newBuffer(Message cmd) {
return new SSHPacket(cmd).putUInt32(recipient); return new SSHPacket(cmd).putUInt32FromInt(recipient);
} }
protected void receiveInto(ChannelInputStream stream, SSHPacket buf) protected void receiveInto(ChannelInputStream stream, SSHPacket buf)

View File

@@ -151,7 +151,7 @@ public final class ChannelInputStream
if (adjustment > 0) { if (adjustment > 0) {
log.debug("Sending SSH_MSG_CHANNEL_WINDOW_ADJUST to #{} for {} bytes", chan.getRecipient(), adjustment); log.debug("Sending SSH_MSG_CHANNEL_WINDOW_ADJUST to #{} for {} bytes", chan.getRecipient(), adjustment);
trans.write(new SSHPacket(Message.CHANNEL_WINDOW_ADJUST) trans.write(new SSHPacket(Message.CHANNEL_WINDOW_ADJUST)
.putUInt32(chan.getRecipient()).putUInt32(adjustment)); .putUInt32FromInt(chan.getRecipient()).putUInt32(adjustment));
win.expand(adjustment); win.expand(adjustment);
} }
} }

View File

@@ -90,7 +90,7 @@ public final class ChannelOutputStream extends OutputStream implements ErrorNoti
packet.wpos(headerOffset); packet.wpos(headerOffset);
packet.putMessageID(Message.CHANNEL_DATA); packet.putMessageID(Message.CHANNEL_DATA);
packet.putUInt32(chan.getRecipient()); packet.putUInt32FromInt(chan.getRecipient());
packet.putUInt32(writeNow); packet.putUInt32(writeNow);
packet.wpos(dataOffset + writeNow); packet.wpos(dataOffset + writeNow);

View File

@@ -25,6 +25,25 @@ import static org.junit.Assert.*;
public class BufferTest { 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 // Issue 72: previously, it entered an infinite loop trying to establish the buffer size
@Test @Test
public void shouldThrowOnTooLargeCapacity() { public void shouldThrowOnTooLargeCapacity() {