This is an automated email from the ASF dual-hosted git repository. twolf pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
The following commit(s) were added to refs/heads/master by this push: new c411efb83 Fix Buffer.getShort() c411efb83 is described below commit c411efb834e9fc70aaf0494fa1cbe81c4b4bc24f Author: Thomas Wolf <tw...@apache.org> AuthorDate: Thu Aug 17 21:16:24 2023 +0200 Fix Buffer.getShort() --- .../java/org/apache/sshd/common/util/buffer/Buffer.java | 4 ++-- .../org/apache/sshd/common/util/buffer/BufferTest.java | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java b/sshd-common/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java index 17f962d07..c9d73b269 100644 --- a/sshd-common/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java +++ b/sshd-common/src/main/java/org/apache/sshd/common/util/buffer/Buffer.java @@ -287,8 +287,8 @@ public abstract class Buffer implements Readable { public short getShort() { ensureAvailable(Short.BYTES); getRawBytes(workBuf, 0, Short.BYTES); - short v = (short) ((workBuf[1] << Byte.SIZE) & 0xFF00); - v |= (short) (workBuf[0] & 0xF); + short v = (short) ((workBuf[0] << Byte.SIZE) & 0xFF00); + v |= (short) (workBuf[1] & 0xFF); return v; } diff --git a/sshd-common/src/test/java/org/apache/sshd/common/util/buffer/BufferTest.java b/sshd-common/src/test/java/org/apache/sshd/common/util/buffer/BufferTest.java index 22b04a102..8e2be33a3 100644 --- a/sshd-common/src/test/java/org/apache/sshd/common/util/buffer/BufferTest.java +++ b/sshd-common/src/test/java/org/apache/sshd/common/util/buffer/BufferTest.java @@ -110,4 +110,18 @@ public class BufferTest extends JUnitTestSupport { assertFalse(e instanceof OutOfMemoryError); assertEquals(8, buffer.array().length); } + + @Test + public void testShortPositive() { + ByteArrayBuffer buffer = new ByteArrayBuffer(2); + buffer.putShort(261); + assertEquals(261, buffer.getShort()); + } + + @Test + public void testShortNegative() { + ByteArrayBuffer buffer = new ByteArrayBuffer(2); + buffer.putShort(-2); + assertEquals(-2, buffer.getShort()); + } }