Skip to content

Commit f67b6e5

Browse files
authored
Fixes #544: Fix a bug in reading EXT32 with 2GB size (#545)
* Fixes #544: Fix a bug in reading EXT32 with 2GB size * Add comment
1 parent 4976b7f commit f67b6e5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ public void skipValue(int count)
553553
skipPayload(readNextLength16() + 1);
554554
break;
555555
case EXT32:
556-
skipPayload(readNextLength32() + 1);
556+
int extLen = readNextLength32();
557+
// Skip the first ext type header (1-byte) first in case ext length is Integer.MAX_VALUE
558+
skipPayload(1);
559+
skipPayload(extLen);
557560
break;
558561
case ARRAY16:
559562
count += readNextLength16();
@@ -1474,6 +1477,9 @@ public int unpackBinaryHeader()
14741477
private void skipPayload(int numBytes)
14751478
throws IOException
14761479
{
1480+
if (numBytes < 0) {
1481+
throw new IllegalArgumentException("payload size must be >= 0: " + numBytes);
1482+
}
14771483
while (true) {
14781484
int bufferRemaining = buffer.size() - position;
14791485
if (bufferRemaining >= numBytes) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.msgpack.core
2+
3+
/**
4+
*
5+
*/
6+
class InvalidDataReadTest extends MessagePackSpec {
7+
8+
"Reading long EXT32" in {
9+
// Prepare an EXT32 data with 2GB (Int.MaxValue size) payload for testing the behavior of MessageUnpacker.skipValue()
10+
// Actually preparing 2GB of data, however, is too much for CI, so we create only the header part.
11+
val msgpack = createMessagePackData(p => p.packExtensionTypeHeader(MessagePack.Code.EXT32, Int.MaxValue))
12+
val u = MessagePack.newDefaultUnpacker(msgpack)
13+
try {
14+
// This error will be thrown after reading the header as the input has no EXT32 body
15+
intercept[MessageInsufficientBufferException] {
16+
u.skipValue()
17+
}
18+
}
19+
finally {
20+
u.close()
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)