Skip to content

Commit c619a78

Browse files
committed
add integer & decimal overflow check
1 parent 9060a26 commit c619a78

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

src/main/java/com/alibaba/fastjson/parser/JSONLexerBase.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.Closeable;
1919
import java.math.BigDecimal;
2020
import java.math.BigInteger;
21+
import java.math.MathContext;
2122
import java.util.*;
2223

2324
import com.alibaba.fastjson.JSON;
@@ -458,11 +459,11 @@ public final Number integerValue() throws NumberFormatException {
458459
// Accumulating negatively avoids surprises near MAX_VALUE
459460
digit = charAt(i++) - '0';
460461
if (result < multmin) {
461-
return new BigInteger(numberString());
462+
return new BigInteger(numberString(), 10);
462463
}
463464
result *= 10;
464465
if (result < limit + digit) {
465-
return new BigInteger(numberString());
466+
return new BigInteger(numberString(), 10);
466467
}
467468
result -= digit;
468469
}
@@ -3041,8 +3042,11 @@ public BigDecimal scanDecimal(char seperator) {
30413042
count = bp + offset - start - 1;
30423043
}
30433044

3045+
if (count > 65535) {
3046+
throw new JSONException("decimal overflow");
3047+
}
30443048
char[] chars = this.sub_chars(start, count);
3045-
value = new BigDecimal(chars);
3049+
value = new BigDecimal(chars, 0, chars.length, MathContext.UNLIMITED);
30463050
} else if (chLocal == 'n' && charAt(bp + offset) == 'u' && charAt(bp + offset + 1) == 'l' && charAt(bp + offset + 2) == 'l') {
30473051
matchStat = VALUE_NULL;
30483052
value = null;
@@ -3715,8 +3719,12 @@ public BigDecimal scanFieldDecimal(char[] fieldName) {
37153719
count = bp + offset - start - 1;
37163720
}
37173721

3722+
if (count > 65535) {
3723+
throw new JSONException("scan decimal overflow");
3724+
}
3725+
37183726
char[] chars = this.sub_chars(start, count);
3719-
value = new BigDecimal(chars);
3727+
value = new BigDecimal(chars, 0, chars.length, MathContext.UNLIMITED);
37203728
} else if (chLocal == 'n' &&
37213729
charAt(bp + offset) == 'u' &&
37223730
charAt(bp + offset + 1) == 'l' &&
@@ -3856,8 +3864,12 @@ public BigInteger scanFieldBigInteger(char[] fieldName) {
38563864

38573865
// char[] chars = this.sub_chars(negative ? start + 1 : start, count);
38583866
// value = new BigInteger(chars, )
3867+
if (count > 65535) {
3868+
throw new JSONException("scanInteger overflow");
3869+
}
3870+
38593871
String strVal = this.subString(start, count);
3860-
value = new BigInteger(strVal);
3872+
value = new BigInteger(strVal, 10);
38613873
}
38623874
} else if (chLocal == 'n' &&
38633875
charAt(bp + offset) == 'u' &&
@@ -5150,6 +5162,10 @@ public final void scanNumber() {
51505162
}
51515163
}
51525164

5165+
if (sp > 65535) {
5166+
throw new JSONException("scanNumber overflow");
5167+
}
5168+
51535169
if (ch == 'L') {
51545170
sp++;
51555171
next();

src/main/java/com/alibaba/fastjson/parser/JSONReaderScanner.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.Reader;
2121
import java.io.StringReader;
2222
import java.math.BigDecimal;
23+
import java.math.MathContext;
2324

2425
import com.alibaba.fastjson.JSON;
2526
import com.alibaba.fastjson.JSONException;
@@ -296,7 +297,11 @@ public final BigDecimal decimalValue() {
296297
sp--;
297298
}
298299

299-
return new BigDecimal(buf, offset, sp);
300+
if (sp > 65535) {
301+
throw new JSONException("decimal overflow");
302+
}
303+
304+
return new BigDecimal(buf, offset, sp, MathContext.UNLIMITED);
300305
}
301306

302307
public void close() {

src/main/java/com/alibaba/fastjson/parser/JSONScanner.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.alibaba.fastjson.util.IOUtils;
2222

2323
import java.math.BigDecimal;
24+
import java.math.MathContext;
2425
import java.util.*;
2526

2627
//这个类,为了性能优化做了很多特别处理,一切都是为了性能!!!
@@ -188,14 +189,18 @@ public final BigDecimal decimalValue() {
188189
sp--;
189190
}
190191

192+
if (sp > 65535) {
193+
throw new JSONException("decimal overflow");
194+
}
195+
191196
int offset = np, count = sp;
192197
if (count < sbuf.length) {
193198
text.getChars(offset, offset + count, sbuf, 0);
194-
return new BigDecimal(sbuf, 0, count);
199+
return new BigDecimal(sbuf, 0, count, MathContext.UNLIMITED);
195200
} else {
196201
char[] chars = new char[count];
197202
text.getChars(offset, offset + count, chars, 0);
198-
return new BigDecimal(chars);
203+
return new BigDecimal(chars, 0, chars.length, MathContext.UNLIMITED);
199204
}
200205
}
201206

src/main/java/com/alibaba/fastjson/serializer/BigIntegerCodec.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Type;
2020
import java.math.BigInteger;
2121

22+
import com.alibaba.fastjson.JSONException;
2223
import com.alibaba.fastjson.parser.DefaultJSONParser;
2324
import com.alibaba.fastjson.parser.JSONLexer;
2425
import com.alibaba.fastjson.parser.JSONToken;
@@ -70,6 +71,11 @@ public static <T> T deserialze(DefaultJSONParser parser) {
7071
if (lexer.token() == JSONToken.LITERAL_INT) {
7172
String val = lexer.numberString();
7273
lexer.nextToken(JSONToken.COMMA);
74+
75+
if (val.length() > 65535) {
76+
throw new JSONException("decimal overflow");
77+
}
78+
7379
return (T) new BigInteger(val);
7480
}
7581

src/main/java/com/alibaba/fastjson/util/TypeUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ public static BigDecimal castToBigDecimal(Object value){
324324
if(value instanceof Map && ((Map) value).size() == 0){
325325
return null;
326326
}
327+
328+
if (strVal.length() > 65535) {
329+
throw new JSONException("decimal overflow");
330+
}
327331
return new BigDecimal(strVal);
328332
}
329333

@@ -350,6 +354,11 @@ public static BigInteger castToBigInteger(Object value){
350354
|| "NULL".equals(strVal)){
351355
return null;
352356
}
357+
358+
if (strVal.length() > 65535) {
359+
throw new JSONException("decimal overflow");
360+
}
361+
353362
return new BigInteger(strVal);
354363
}
355364

0 commit comments

Comments
 (0)