Skip to content

Commit 823660f

Browse files
committed
bound Int coercion before expanding BigDecimal to BigInteger
1 parent 4c09620 commit 823660f

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎src/main/java/graphql/scalar/GraphqlIntCoercing.java‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

‎src/test/groovy/graphql/ScalarsIntTest.groovy‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import graphql.schema.CoercingParseLiteralException
88
import graphql.schema.CoercingParseValueException
99
import graphql.schema.CoercingSerializeException
1010
import spock.lang.Specification
11+
import spock.lang.Timeout
1112
import spock.lang.Unroll
1213

1314
import java.util.concurrent.atomic.AtomicInteger
@@ -189,4 +190,16 @@ class ScalarsIntTest extends Specification {
189190
"-1" | _
190191
}
191192

193+
@Timeout(10)
194+
def "parseValue rejects a huge-magnitude BigDecimal from its scale rather than expanding it"() {
195+
// 1E1000000000 is a tiny input but toBigIntegerExact() would try to materialise a BigInteger
196+
// with ~10^9 digits. The value is out of the Int range by magnitude and must be rejected as
197+
// such from the decimal's scale/precision, not by first expanding it.
198+
when:
199+
Scalars.GraphQLInt.getCoercing().parseValue(new BigDecimal("1E1000000000"), GraphQLContext.default, Locale.default)
200+
then:
201+
def e = thrown(CoercingParseValueException)
202+
e.message.contains("integer range")
203+
}
204+
192205
}

0 commit comments

Comments
 (0)