@@ -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 }
0 commit comments