Skip to content

Commit 8790733

Browse files
committed
Fix NPE in MessagePacker#writePayload(java.nio.ByteBuffer)
1 parent 3d8e250 commit 8790733

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,10 @@ public MessagePacker writePayload(ByteBuffer src) throws IOException {
511511
else {
512512
// If the input source is small, simply copy the contents to the buffer
513513
while(src.remaining() > 0) {
514-
if(position >= buffer.size())
514+
if(position >= buffer.size()) {
515515
flush();
516+
}
517+
prepareBuffer();
516518
int writeLen = Math.min(buffer.size() - position, src.remaining());
517519
buffer.putByteBuffer(position, src, writeLen);
518520
position += writeLen;

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.msgpack.core.buffer.{OutputStreamBufferOutput, ArrayBufferInput}
2121
import xerial.core.io.IOUtil
2222

2323
import scala.util.Random
24+
import org.msgpack.value.ValueFactory
2425

2526
/**
2627
*
@@ -102,5 +103,32 @@ class MessagePackerTest extends MessagePackSpec {
102103

103104
}
104105

106+
"pack larger string array than byte buf" taggedAs ("larger-string-array-than-byte-buf") in {
107+
// Based on https://github.com/msgpack/msgpack-java/issues/154
108+
109+
// TODO: Refactor this test code to fit other ones.
110+
def test(bufferSize: Int, stringSize: Int): Boolean = {
111+
val msgpack = new MessagePack(new MessagePack.ConfigBuilder().packerBufferSize(bufferSize).build)
112+
val str = "a" * stringSize
113+
val rawString = ValueFactory.newRawString(str.getBytes("UTF-8"))
114+
val array = ValueFactory.newArray(rawString)
115+
val out = new ByteArrayOutputStream()
116+
val packer = msgpack.newPacker(out)
117+
packer.packValue(array)
118+
packer.close()
119+
out.toByteArray
120+
true
121+
}
122+
123+
val testCases = List(
124+
32 -> 30,
125+
33 -> 31,
126+
32 -> 31,
127+
34 -> 32
128+
)
129+
testCases.foreach{
130+
case (bufferSize, stringSize) => test(bufferSize, stringSize)
131+
}
132+
}
105133
}
106134
}

0 commit comments

Comments
 (0)