Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MessageUnpacker#unpackString in case that string length is more than... #215

Merged
merged 1 commit into from
Apr 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,6 @@ public String unpackString() throws IOException {

if(cr.isOverflow()) {
// The output CharBuffer has insufficient space
readLen = bb.limit() - bb.remaining();
decoder.reset();
}

Expand All @@ -1005,9 +1004,10 @@ public String unpackString() throws IOException {
sb.append(decodeBuffer);

decodeBuffer.clear();
cursor += readLen;
consume(readLen);
}

cursor += readLen;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no guarantee that the read length from the input always matches Math.min(buffer.size() - position, strLen-cursor) when OVERFLOW (output buffer has insufficient space) happens. When OVERFLOW occurs, the input data might be partially read. That is why I compute readLen as bb.limit() - bb.remaining().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I was wrong. readLen can be retrieved by bb.position() since we created a new ByteBuffer at

ByteBuffer bb = buffer.toByteBuffer(position, readLen);

whose initial position is 0.

consume(readLen);
}

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,5 +618,33 @@ class MessageUnpackerTest extends MessagePackSpec {
checkFile(u)
u.close
}

"parse message large packed data" taggedAs("unpack") in {
def createLargeData(stringLength: Int): Array[Byte] = {
val out = new ByteArrayOutputStream()
val packer = msgpack.newPacker(out)

packer
.packArrayHeader(2)
.packString("l" * stringLength)
.packInt(1)

packer.close()

out.toByteArray
}

Seq(8191, 8192, 8193, 16383, 16384, 16385).foreach { n =>
val arr = createLargeData(n)

val unpacker = msgpack.newUnpacker(arr)

unpacker.unpackArrayHeader shouldBe 2
unpacker.unpackString.length shouldBe n
unpacker.unpackInt shouldBe 1

unpacker.getTotalReadBytes shouldBe arr.length
}
}
}
}