@@ -31,6 +31,9 @@ public class GraphqlIntCoercing implements Coercing<Integer, Integer> {
3131 private static final BigInteger INT_MAX = BigInteger .valueOf (Integer .MAX_VALUE );
3232 private static final BigInteger INT_MIN = BigInteger .valueOf (Integer .MIN_VALUE );
3333
34+ // Integer.MAX_VALUE / Integer.MIN_VALUE both have 10 significant digits
35+ private static final int INT_MAX_DIGITS = 10 ;
36+
3437 private Integer convertImpl (Object input ) {
3538 if (input instanceof Integer ) {
3639 return (Integer ) input ;
@@ -74,7 +77,7 @@ private Integer parseValueImpl(@NonNull Object input, @NonNull Locale locale) {
7477 return (Integer ) input ;
7578 }
7679
77- BigInteger result = convertParseValueImpl (input );
80+ BigInteger result = convertParseValueImpl (input , locale );
7881 if (result == null ) {
7982 throw new CoercingParseValueException (
8083 i18nMsg (locale , "Int.notInt" , typeName (input ))
@@ -88,14 +91,26 @@ private Integer parseValueImpl(@NonNull Object input, @NonNull Locale locale) {
8891 return result .intValueExact ();
8992 }
9093
91- private BigInteger convertParseValueImpl (Object input ) {
94+ private BigInteger convertParseValueImpl (Object input , @ NonNull Locale locale ) {
9295 BigDecimal value ;
9396 try {
9497 value = new BigDecimal (input .toString ());
9598 } catch (NumberFormatException e ) {
9699 return null ;
97100 }
98101
102+ // A value whose integer part has more digits than Integer can hold is out of range by
103+ // definition. Decide that from the decimal's scale/precision instead of letting
104+ // toBigIntegerExact() expand it: a variable value like 1E1000000000 arrives here as a
105+ // BigDecimal and would otherwise materialise a BigInteger with ~10^9 digits (CPU/heap blow
106+ // up) before the range check in parseValueImpl ever runs. Use long math because scale can
107+ // be close to Integer.MIN_VALUE.
108+ if ((long ) value .precision () - value .scale () > INT_MAX_DIGITS ) {
109+ throw new CoercingParseValueException (
110+ i18nMsg (locale , "Int.outsideRange" , value .toString ())
111+ );
112+ }
113+
99114 try {
100115 return value .toBigIntegerExact ();
101116 } catch (ArithmeticException e ) {
0 commit comments