Skip to content

Commit 0c0c41f

Browse files
authored
Merge pull request #392 from msgpack/add-buffer-null-check
Fix #391: Add missing buffer null check
2 parents 8e89b99 + cf0dfac commit 0c0c41f

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,9 @@ public MessagePacker writePayload(byte[] src)
729729
public MessagePacker writePayload(byte[] src, int off, int len)
730730
throws IOException
731731
{
732-
if (buffer.size() - position < len || len > bufferFlushThreshold) {
732+
if (buffer == null || buffer.size() - position < len || len > bufferFlushThreshold) {
733733
flush(); // call flush before write
734+
// Directly write payload to the output without using the buffer
734735
out.write(src, off, len);
735736
totalFlushBytes += len;
736737
}
@@ -770,8 +771,9 @@ public MessagePacker addPayload(byte[] src)
770771
public MessagePacker addPayload(byte[] src, int off, int len)
771772
throws IOException
772773
{
773-
if (buffer.size() - position < len || len > bufferFlushThreshold) {
774+
if (buffer == null || buffer.size() - position < len || len > bufferFlushThreshold) {
774775
flush(); // call flush before add
776+
// Directly add the payload without using the buffer
775777
out.add(src, off, len);
776778
totalFlushBytes += len;
777779
}

msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,17 @@ class MessagePackerTest extends MessagePackSpec {
317317
val s = unpacker.unpackString()
318318
s shouldBe "small string"
319319
}
320+
321+
"write raw binary" taggedAs("raw-binary") in {
322+
val packer = new MessagePack.PackerConfig().newBufferPacker()
323+
val msg = Array[Byte](-127, -92, 116, 121, 112, 101, -92, 112, 105, 110, 103)
324+
packer.writePayload(msg)
325+
}
326+
327+
"append raw binary" taggedAs("append-raw-binary") in {
328+
val packer = new MessagePack.PackerConfig().newBufferPacker()
329+
val msg = Array[Byte](-127, -92, 116, 121, 112, 101, -92, 112, 105, 110, 103)
330+
packer.addPayload(msg)
331+
}
332+
320333
}

0 commit comments

Comments
 (0)