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

Fixes #544: Fix a bug in reading EXT32 with 2GB size #545

Merged
merged 2 commits into from
Feb 16, 2021
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 @@ -553,7 +553,10 @@ public void skipValue(int count)
skipPayload(readNextLength16() + 1);
break;
case EXT32:
skipPayload(readNextLength32() + 1);
int extLen = readNextLength32();
// Skip the first ext type header (1-byte) first in case ext length is Integer.MAX_VALUE
skipPayload(1);
skipPayload(extLen);
break;
case ARRAY16:
count += readNextLength16();
Expand Down Expand Up @@ -1474,6 +1477,9 @@ public int unpackBinaryHeader()
private void skipPayload(int numBytes)
throws IOException
{
if (numBytes < 0) {
throw new IllegalArgumentException("payload size must be >= 0: " + numBytes);
}
while (true) {
int bufferRemaining = buffer.size() - position;
if (bufferRemaining >= numBytes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.msgpack.core

/**
*
*/
class InvalidDataReadTest extends MessagePackSpec {

"Reading long EXT32" in {
// Prepare an EXT32 data with 2GB (Int.MaxValue size) payload for testing the behavior of MessageUnpacker.skipValue()
// Actually preparing 2GB of data, however, is too much for CI, so we create only the header part.
val msgpack = createMessagePackData(p => p.packExtensionTypeHeader(MessagePack.Code.EXT32, Int.MaxValue))
val u = MessagePack.newDefaultUnpacker(msgpack)
try {
// This error will be thrown after reading the header as the input has no EXT32 body
intercept[MessageInsufficientBufferException] {
Copy link
Member

Choose a reason for hiding this comment

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

The expectation of this test case is unclear to me. I guess you want to avoid 2GB memory allocation in this test, though. How about adding a comment to describe your intention?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added more comment to explain why we need to use this a bit weird test code

u.skipValue()
}
}
finally {
u.close()
}
}
}