Skip to content

Commit 9ae4936

Browse files
author
elasticsearchmachine
committed
Merge remote-tracking branch 'origin/main' into lucene_snapshot
2 parents 2641103 + 2f2ddad commit 9ae4936

File tree

3 files changed

+87
-8
lines changed

3 files changed

+87
-8
lines changed

muted-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ tests:
288288
- class: org.elasticsearch.xpack.inference.InferenceCrudIT
289289
method: testSupportedStream
290290
issue: https://github.com/elastic/elasticsearch/issues/113430
291+
- class: org.elasticsearch.xpack.spatial.search.GeoGridAggAndQueryConsistencyIT
292+
method: testGeoShapeGeoTile
293+
issue: https://github.com/elastic/elasticsearch/issues/115717
291294

292295
# Examples:
293296
#

server/src/main/java/org/elasticsearch/common/settings/Setting.java

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.elasticsearch.xcontent.XContentType;
3535

3636
import java.io.IOException;
37+
import java.math.BigInteger;
3738
import java.time.Instant;
3839
import java.util.Arrays;
3940
import java.util.Collections;
@@ -1485,27 +1486,68 @@ public static int parseInt(String s, int minValue, int maxValue, String key) {
14851486
}
14861487

14871488
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+
}
14891506
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);
14921508
}
14931509
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);
14961511
}
14971512
return value;
14981513
}
14991514

15001515
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+
}
15021533
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);
15051535
}
15061536
return value;
15071537
}
15081538

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+
15091551
public static Setting<Integer> intSetting(String key, int defaultValue, Property... properties) {
15101552
return intSetting(key, defaultValue, Integer.MIN_VALUE, properties);
15111553
}

server/src/test/java/org/elasticsearch/common/settings/SettingTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,4 +1522,38 @@ public void testDeprecationPropertyValidation() {
15221522
() -> Setting.boolSetting("a.bool.setting", true, Property.DeprecatedWarning, Property.IndexSettingDeprecatedInV7AndRemovedInV8)
15231523
);
15241524
}
1525+
1526+
public void testIntSettingBounds() {
1527+
Setting<Integer> setting = Setting.intSetting("int.setting", 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
1528+
var e = expectThrows(
1529+
IllegalArgumentException.class,
1530+
() -> setting.get(Settings.builder().put("int.setting", "2147483648").build())
1531+
);
1532+
assertThat(e.getMessage(), equalTo("Failed to parse value [2147483648] for setting [int.setting] must be <= 2147483647"));
1533+
var e2 = expectThrows(
1534+
IllegalArgumentException.class,
1535+
() -> setting.get(Settings.builder().put("int.setting", "-2147483649").build())
1536+
);
1537+
assertThat(e2.getMessage(), equalTo("Failed to parse value [-2147483649] for setting [int.setting] must be >= -2147483648"));
1538+
}
1539+
1540+
public void testLongSettingBounds() {
1541+
Setting<Long> setting = Setting.longSetting("long.setting", 0, Long.MIN_VALUE);
1542+
var e = expectThrows(
1543+
IllegalArgumentException.class,
1544+
() -> setting.get(Settings.builder().put("long.setting", "9223372036854775808").build())
1545+
);
1546+
assertThat(
1547+
e.getMessage(),
1548+
equalTo("Failed to parse value [9223372036854775808] for setting [long.setting] must be <= 9223372036854775807")
1549+
);
1550+
var e2 = expectThrows(
1551+
IllegalArgumentException.class,
1552+
() -> setting.get(Settings.builder().put("long.setting", "-9223372036854775809").build())
1553+
);
1554+
assertThat(
1555+
e2.getMessage(),
1556+
equalTo("Failed to parse value [-9223372036854775809] for setting [long.setting] must be >= -9223372036854775808")
1557+
);
1558+
}
15251559
}

0 commit comments

Comments
 (0)