Skip to content

Commit 9cc49c0

Browse files
committed
Fix STR8 encoding and underflow check in packString
1 parent 535ffa2 commit 9cc49c0

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.msgpack.core;
1717

1818
import org.msgpack.core.buffer.ArrayBufferInput;
19+
import org.msgpack.core.buffer.ArrayBufferOutput;
1920
import org.msgpack.core.buffer.ChannelBufferInput;
2021
import org.msgpack.core.buffer.ChannelBufferOutput;
2122
import org.msgpack.core.buffer.InputStreamBufferInput;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ private int encodeStringToBufferAt(int pos, String s)
457457
ByteBuffer bb = buffer.sliceAsByteBuffer(pos, buffer.size() - pos);
458458
int startPosition = bb.position();
459459
CharBuffer in = CharBuffer.wrap(s);
460+
encoder.reset();
460461
CoderResult cr = encoder.encode(in, bb, true);
461462
if (cr.isError()) {
462463
try {
@@ -466,7 +467,8 @@ private int encodeStringToBufferAt(int pos, String s)
466467
throw new MessageStringCodingException(e);
467468
}
468469
}
469-
if (cr.isUnderflow() || cr.isOverflow()) {
470+
if (!cr.isUnderflow() || cr.isOverflow()) {
471+
// Underflow should be on to ensure all of the input string is encoded
470472
return -1;
471473
}
472474
return bb.position() - startPosition;
@@ -499,7 +501,7 @@ else if (s.length() < (1 << 8)) {
499501
// keep 2-byte header region and write raw string
500502
int written = encodeStringToBufferAt(position + 2, s);
501503
if (written >= 0) {
502-
if (written < (1 << 8)) {
504+
if (str8FormatSupport && written < (1 << 8)) {
503505
buffer.putByte(position++, STR8);
504506
buffer.putByte(position++, (byte) written);
505507
position += written;

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,39 @@ class MessagePackerTest extends MessagePackSpec {
282282
.writePayload(payload)
283283
.close()
284284
}
285+
286+
"pack small string with STR8" in {
287+
val packer = new PackerConfig().newBufferPacker()
288+
packer.packString("Hello. This is a string longer than 32 characters!")
289+
val b = packer.toByteArray
290+
291+
val unpacker = MessagePack.newDefaultUnpacker(b)
292+
val f = unpacker.getNextFormat
293+
f shouldBe MessageFormat.STR8
294+
}
295+
296+
"be able to disable STR8 for backward compatibility" in {
297+
val config = new PackerConfig()
298+
.withSmallStringOptimizationThreshold(0) // Disable small string optimiztion
299+
.withStr8FormatSupport(false)
300+
301+
{
302+
val packer = config.newBufferPacker()
303+
packer.packString("Hello. This is a string longer than 32 characters!")
304+
val unpacker = MessagePack.newDefaultUnpacker(packer.toByteArray)
305+
val f = unpacker.getNextFormat
306+
f shouldBe MessageFormat.STR16
307+
}
308+
309+
{
310+
val packer2 = config.newBufferPacker()
311+
packer2.packString("small string")
312+
packer2.flush()
313+
val unpacker2 = MessagePack.newDefaultUnpacker(packer2.toByteArray)
314+
val f2 = unpacker2.getNextFormat
315+
f2 shouldNot be (MessageFormat.STR8)
316+
val s = unpacker2.unpackString()
317+
s shouldBe "small string"
318+
}
319+
}
285320
}

0 commit comments

Comments
 (0)