| 
34 | 34 | import org.elasticsearch.xcontent.XContentType;  | 
35 | 35 | 
 
  | 
36 | 36 | import java.io.IOException;  | 
 | 37 | +import java.math.BigInteger;  | 
37 | 38 | import java.time.Instant;  | 
38 | 39 | import java.util.Arrays;  | 
39 | 40 | import java.util.Collections;  | 
@@ -1485,27 +1486,68 @@ public static int parseInt(String s, int minValue, int maxValue, String key) {  | 
1485 | 1486 |     }  | 
1486 | 1487 | 
 
  | 
1487 | 1488 |     public static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) {  | 
1488 |  | -        int value = Integer.parseInt(s);  | 
 | 1489 | +        int value;  | 
 | 1490 | +        try {  | 
 | 1491 | +            value = Integer.parseInt(s);  | 
 | 1492 | +        } catch (NumberFormatException e) {  | 
 | 1493 | +            // check if value is a number or garbage  | 
 | 1494 | +            try {  | 
 | 1495 | +                var bi = new BigInteger(s);  | 
 | 1496 | +                // it's a number, so check which bound it is outside  | 
 | 1497 | +                if (bi.compareTo(BigInteger.valueOf(minValue)) < 0) {  | 
 | 1498 | +                    throw newNumericBoundsException(s, key, isFiltered, ">=", minValue);  | 
 | 1499 | +                } else {  | 
 | 1500 | +                    throw newNumericBoundsException(s, key, isFiltered, "<=", maxValue);  | 
 | 1501 | +                }  | 
 | 1502 | +            } catch (NumberFormatException e2) {  | 
 | 1503 | +                throw e; // it's garbage, use the original exception  | 
 | 1504 | +            }  | 
 | 1505 | +        }  | 
1489 | 1506 |         if (value < minValue) {  | 
1490 |  | -            String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue;  | 
1491 |  | -            throw new IllegalArgumentException(err);  | 
 | 1507 | +            throw newNumericBoundsException(s, key, isFiltered, ">=", minValue);  | 
1492 | 1508 |         }  | 
1493 | 1509 |         if (value > maxValue) {  | 
1494 |  | -            String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue;  | 
1495 |  | -            throw new IllegalArgumentException(err);  | 
 | 1510 | +            throw newNumericBoundsException(s, key, isFiltered, "<=", maxValue);  | 
1496 | 1511 |         }  | 
1497 | 1512 |         return value;  | 
1498 | 1513 |     }  | 
1499 | 1514 | 
 
  | 
1500 | 1515 |     static long parseLong(String s, long minValue, String key, boolean isFiltered) {  | 
1501 |  | -        long value = Long.parseLong(s);  | 
 | 1516 | +        long value;  | 
 | 1517 | +        try {  | 
 | 1518 | +            value = Long.parseLong(s);  | 
 | 1519 | +        } catch (NumberFormatException e) {  | 
 | 1520 | +            // check if value is a number or garbage  | 
 | 1521 | +            try {  | 
 | 1522 | +                var bi = new BigInteger(s);  | 
 | 1523 | +                // it's a number, so check which bound it is outside  | 
 | 1524 | +                if (bi.compareTo(BigInteger.valueOf(minValue)) < 0) {  | 
 | 1525 | +                    throw newNumericBoundsException(s, key, isFiltered, ">=", minValue);  | 
 | 1526 | +                } else {  | 
 | 1527 | +                    throw newNumericBoundsException(s, key, isFiltered, "<=", Long.MAX_VALUE);  | 
 | 1528 | +                }  | 
 | 1529 | +            } catch (NumberFormatException e2) {  | 
 | 1530 | +                throw e; // it's garbage, use the original exception  | 
 | 1531 | +            }  | 
 | 1532 | +        }  | 
1502 | 1533 |         if (value < minValue) {  | 
1503 |  | -            String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue;  | 
1504 |  | -            throw new IllegalArgumentException(err);  | 
 | 1534 | +            throw newNumericBoundsException(s, key, isFiltered, ">=", minValue);  | 
1505 | 1535 |         }  | 
1506 | 1536 |         return value;  | 
1507 | 1537 |     }  | 
1508 | 1538 | 
 
  | 
 | 1539 | +    private static IllegalArgumentException newNumericBoundsException(String s, String key, boolean isFiltered, String type, long bound) {  | 
 | 1540 | +        String err = "Failed to parse value"  | 
 | 1541 | +            + (isFiltered ? "" : " [" + s + "]")  | 
 | 1542 | +            + " for setting ["  | 
 | 1543 | +            + key  | 
 | 1544 | +            + "] must be "  | 
 | 1545 | +            + type  | 
 | 1546 | +            + " "  | 
 | 1547 | +            + bound;  | 
 | 1548 | +        throw new IllegalArgumentException(err);  | 
 | 1549 | +    }  | 
 | 1550 | + | 
1509 | 1551 |     public static Setting<Integer> intSetting(String key, int defaultValue, Property... properties) {  | 
1510 | 1552 |         return intSetting(key, defaultValue, Integer.MIN_VALUE, properties);  | 
1511 | 1553 |     }  | 
 | 
0 commit comments