Skip to content

Commit 1cf85a3

Browse files
committed
refined AbstractUnpacker and MessagePackUnpacker classes again
1 parent b3603a3 commit 1cf85a3

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

src/main/java/org/msgpack/unpacker/AbstractUnpacker.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,24 @@ public void resetReadByteCount() {
117117
}
118118

119119
public void setRawSizeLimit(long size) {
120-
if (size > 0 && size >= 67108864) {
120+
if (size < 32) {
121+
rawSizeLimit = 32;
122+
} else {
121123
rawSizeLimit = size;
122124
}
123125
}
124126

125127
public void setArraySizeLimit(int size) {
126-
if(size < 0 || size >= 4096) {
127-
arraySizeLimit = 4096;
128+
if (size < 16) {
129+
arraySizeLimit = 16;
128130
} else {
129131
arraySizeLimit = size;
130132
}
131133
}
132134

133135
public void setMapSizeLimit(int size) {
134-
if(size < 0 || size >= 4096) {
135-
mapSizeLimit = 4096;
136+
if (size < 16) {
137+
mapSizeLimit = 16;
136138
} else {
137139
mapSizeLimit = size;
138140
}

src/main/java/org/msgpack/unpacker/MessagePackUnpacker.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ private boolean readOneWithoutStackLarge(Accept a, final int b) throws IOExcepti
212212
headByte = REQUIRE_TO_READ_HEAD;
213213
return true;
214214
}
215+
if (count >= rawSizeLimit) {
216+
String reason = String.format("Size of raw (%d) over limit at %d",
217+
new Object[] { count, rawSizeLimit });
218+
throw new SizeLimitException(reason);
219+
}
215220
in.advance();
216221
if (!tryReferRawBody(a, count)) {
217222
readRawBody(count);
@@ -224,17 +229,17 @@ private boolean readOneWithoutStackLarge(Accept a, final int b) throws IOExcepti
224229
case 0xdb: // raw 32
225230
{
226231
int count = in.getInt();
227-
if (count < 0 || count > rawSizeLimit) {
228-
String reason = String.format("Size of raw (%d) over limit at %d",
229-
new Object[] { count, rawSizeLimit });
230-
throw new SizeLimitException(reason);
231-
}
232232
if (count == 0) {
233233
a.acceptEmptyRaw();
234234
in.advance();
235235
headByte = REQUIRE_TO_READ_HEAD;
236236
return true;
237237
}
238+
if (count < 0 || count >= rawSizeLimit) {
239+
String reason = String.format("Size of raw (%d) over limit at %d",
240+
new Object[] { count, rawSizeLimit });
241+
throw new SizeLimitException(reason);
242+
}
238243
in.advance();
239244
if (!tryReferRawBody(a, count)) {
240245
readRawBody(count);
@@ -247,7 +252,7 @@ private boolean readOneWithoutStackLarge(Accept a, final int b) throws IOExcepti
247252
case 0xdc: // array 16
248253
{
249254
int count = in.getShort() & 0xffff;
250-
if (count > arraySizeLimit) {
255+
if (count >= arraySizeLimit) {
251256
String reason = String.format("Size of array (%d) over limit at %d",
252257
new Object[] { count, arraySizeLimit });
253258
throw new SizeLimitException(reason);
@@ -262,7 +267,7 @@ private boolean readOneWithoutStackLarge(Accept a, final int b) throws IOExcepti
262267
case 0xdd: // array 32
263268
{
264269
int count = in.getInt();
265-
if (count < 0 || count > arraySizeLimit) {
270+
if (count < 0 || count >= arraySizeLimit) {
266271
String reason = String.format("Size of array (%d) over limit at %d",
267272
new Object[] { count, arraySizeLimit });
268273
throw new SizeLimitException(reason);
@@ -277,7 +282,7 @@ private boolean readOneWithoutStackLarge(Accept a, final int b) throws IOExcepti
277282
case 0xde: // map 16
278283
{
279284
int count = in.getShort() & 0xffff;
280-
if (count > mapSizeLimit) {
285+
if (count >= mapSizeLimit) {
281286
String reason = String.format("Size of map (%d) over limit at %d",
282287
new Object[] { count, mapSizeLimit });
283288
throw new SizeLimitException(reason);
@@ -292,7 +297,7 @@ private boolean readOneWithoutStackLarge(Accept a, final int b) throws IOExcepti
292297
case 0xdf: // map 32
293298
{
294299
int count = in.getInt();
295-
if (count < 0 || count > mapSizeLimit) {
300+
if (count < 0 || count >= mapSizeLimit) {
296301
String reason = String.format("Size of map (%d) over limit at %d",
297302
new Object[] { count, mapSizeLimit });
298303
throw new SizeLimitException(reason);

0 commit comments

Comments
 (0)