Skip to content

Commit 0c46cc1

Browse files
Sean LearySean Leary
authored andcommitted
1063-alt-min initial commit
1 parent c91d821 commit 0c46cc1

3 files changed

Lines changed: 409 additions & 5 deletions

File tree

‎src/main/java/org/json/JSONObject.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,11 @@ public Boolean optBooleanObject(String key, Boolean defaultValue) {
13141314
*/
13151315
public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
13161316
Object val = this.opt(key);
1317-
return objectToBigDecimal(val, defaultValue);
1317+
try {
1318+
return objectToBigDecimal(val, defaultValue);
1319+
} catch (JSONException jsonException) {
1320+
return defaultValue;
1321+
}
13181322
}
13191323

13201324
/**
@@ -1363,6 +1367,9 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
13631367
}
13641368
// don't check if it's a string in case of unchecked Number subclasses
13651369
try {
1370+
if (val.toString().length() > 1000) {
1371+
throw new JSONException("Value too long for BigDecimal");
1372+
}
13661373
return new BigDecimal(val.toString());
13671374
} catch (Exception e) {
13681375
return defaultValue;
@@ -1382,7 +1389,11 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue, boolea
13821389
*/
13831390
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
13841391
Object val = this.opt(key);
1385-
return objectToBigInteger(val, defaultValue);
1392+
try {
1393+
return objectToBigInteger(val, defaultValue);
1394+
} catch (JSONException jsonException) {
1395+
return defaultValue;
1396+
}
13861397
}
13871398

13881399
/**
@@ -1421,6 +1432,9 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
14211432
* that type cast support that may truncate the decimal.
14221433
*/
14231434
final String valStr = val.toString();
1435+
if (valStr.length() > 1000) {
1436+
throw new JSONException("Value too long for BigInteger or BigDecimal");
1437+
}
14241438
if(isDecimalNotation(valStr)) {
14251439
return new BigDecimal(valStr).toBigInteger();
14261440
}
@@ -2700,7 +2714,9 @@ public static Object stringToValue(String string) {
27002714
char initial = string.charAt(0);
27012715
if ((initial >= '0' && initial <= '9') || initial == '-') {
27022716
try {
2703-
return stringToNumber(string);
2717+
if (string.length() <= 1000) {
2718+
return stringToNumber(string);
2719+
}
27042720
} catch (Exception ignore) {
27052721
// Do nothing
27062722
}

‎src/main/java/org/json/XML.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
441441
jsonObject.accumulate(config.getcDataTagName(),
442442
config.isKeepStrings() ? ((String) token) : obj);
443443
} else {
444-
jsonObject.accumulate(config.getcDataTagName(), stringToValue((String) token));
444+
jsonObject.accumulate(config.getcDataTagName(), token);
445445
}
446446
}
447447
}
@@ -650,7 +650,9 @@ public static Object stringToValue(String string) {
650650
char initial = string.charAt(0);
651651
if ((initial >= '0' && initial <= '9') || initial == '-') {
652652
try {
653-
return stringToNumber(string);
653+
if (string.length() <= 1000) {
654+
return stringToNumber(string);
655+
}
654656
} catch (Exception ignore) {
655657
}
656658
}

0 commit comments

Comments
 (0)