|
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; |
@@ -1507,27 +1508,68 @@ public static int parseInt(String s, int minValue, int maxValue, String key) { |
1507 | 1508 | } |
1508 | 1509 |
|
1509 | 1510 | public static int parseInt(String s, int minValue, int maxValue, String key, boolean isFiltered) { |
1510 | | - int value = Integer.parseInt(s); |
| 1511 | + int value; |
| 1512 | + try { |
| 1513 | + value = Integer.parseInt(s); |
| 1514 | + } catch (NumberFormatException e) { |
| 1515 | + // check if value is a number or garbage |
| 1516 | + try { |
| 1517 | + var bi = new BigInteger(s); |
| 1518 | + // it's a number, so check which bound it is outside |
| 1519 | + if (bi.compareTo(BigInteger.valueOf(minValue)) < 0) { |
| 1520 | + throw newNumericBoundsException(s, key, isFiltered, ">=", minValue); |
| 1521 | + } else { |
| 1522 | + throw newNumericBoundsException(s, key, isFiltered, "<=", maxValue); |
| 1523 | + } |
| 1524 | + } catch (NumberFormatException e2) { |
| 1525 | + throw e; // it's garbage, use the original exception |
| 1526 | + } |
| 1527 | + } |
1511 | 1528 | if (value < minValue) { |
1512 | | - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; |
1513 | | - throw new IllegalArgumentException(err); |
| 1529 | + throw newNumericBoundsException(s, key, isFiltered, ">=", minValue); |
1514 | 1530 | } |
1515 | 1531 | if (value > maxValue) { |
1516 | | - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be <= " + maxValue; |
1517 | | - throw new IllegalArgumentException(err); |
| 1532 | + throw newNumericBoundsException(s, key, isFiltered, "<=", maxValue); |
1518 | 1533 | } |
1519 | 1534 | return value; |
1520 | 1535 | } |
1521 | 1536 |
|
1522 | 1537 | static long parseLong(String s, long minValue, String key, boolean isFiltered) { |
1523 | | - long value = Long.parseLong(s); |
| 1538 | + long value; |
| 1539 | + try { |
| 1540 | + value = Long.parseLong(s); |
| 1541 | + } catch (NumberFormatException e) { |
| 1542 | + // check if value is a number or garbage |
| 1543 | + try { |
| 1544 | + var bi = new BigInteger(s); |
| 1545 | + // it's a number, so check which bound it is outside |
| 1546 | + if (bi.compareTo(BigInteger.valueOf(minValue)) < 0) { |
| 1547 | + throw newNumericBoundsException(s, key, isFiltered, ">=", minValue); |
| 1548 | + } else { |
| 1549 | + throw newNumericBoundsException(s, key, isFiltered, "<=", Long.MAX_VALUE); |
| 1550 | + } |
| 1551 | + } catch (NumberFormatException e2) { |
| 1552 | + throw e; // it's garbage, use the original exception |
| 1553 | + } |
| 1554 | + } |
1524 | 1555 | if (value < minValue) { |
1525 | | - String err = "Failed to parse value" + (isFiltered ? "" : " [" + s + "]") + " for setting [" + key + "] must be >= " + minValue; |
1526 | | - throw new IllegalArgumentException(err); |
| 1556 | + throw newNumericBoundsException(s, key, isFiltered, ">=", minValue); |
1527 | 1557 | } |
1528 | 1558 | return value; |
1529 | 1559 | } |
1530 | 1560 |
|
| 1561 | + private static IllegalArgumentException newNumericBoundsException(String s, String key, boolean isFiltered, String type, long bound) { |
| 1562 | + String err = "Failed to parse value" |
| 1563 | + + (isFiltered ? "" : " [" + s + "]") |
| 1564 | + + " for setting [" |
| 1565 | + + key |
| 1566 | + + "] must be " |
| 1567 | + + type |
| 1568 | + + " " |
| 1569 | + + bound; |
| 1570 | + throw new IllegalArgumentException(err); |
| 1571 | + } |
| 1572 | + |
1531 | 1573 | public static Setting<Integer> intSetting(String key, int defaultValue, Property... properties) { |
1532 | 1574 | return intSetting(key, defaultValue, Integer.MIN_VALUE, properties); |
1533 | 1575 | } |
|
0 commit comments