-
Notifications
You must be signed in to change notification settings - Fork 24.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stricter validation for min/max values for whole numbers #26137
Merged
jpountz
merged 29 commits into
elastic:master
from
fred84:number_field_type_out_of_range_validation
Aug 21, 2017
Merged
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
2d0e5c1
validate half float values
fred84 c0fff6f
Merge branch 'master' into 25534_reject_out_of_range_numbers
fred84 6e0a6ea
test upper bound for numeric mapper
fred84 7d4f315
merge master
fred84 d1ebd6f
test for upper bound for float, double and half_float
fred84 1358fed
Merge branch 'master' into 25533_reject_out_of_range_numbers
fred84 44983d8
more tests on NaN and Infinity for NumberFieldMapper
fred84 d072e69
Merge branch 'master' into 25534_reject_out_of_range_numbers
fred84 ecf3424
fix checkstyle errors
fred84 b5d231d
minor renaming
fred84 187982a
resolve merge conflict and cleanup NumberFieldMapper out of range tests
fred84 d236158
Merge remote-tracking branch 'upstream/master' into 25534_reject_out_…
fred84 7423c4d
comments for disabled test
fred84 bced91e
tests for out of range values for long
fred84 7e07a63
min/max validation for short and integer numbertypes
fred84 61d8585
Merge remote-tracking branch 'upstream/master' into number_field_type…
fred84 8e88c9d
Merge remote-tracking branch 'upstream/master' into 25534_reject_out_…
fred84 11ce7c8
tests for byte/short/integer/long removed and will be added in separa…
fred84 3902911
remove unused import
fred84 eba60b7
Merge remote-tracking branch 'upstream/master' into number_field_type…
fred84 4dd7a83
25534_reject_out_of_range_numbers merged
fred84 8901920
merge and resolve conflict
fred84 1a32b05
tests for negative values
fred84 efe2adf
min/max validation for short/int/long
fred84 0eb0c01
Merge remote-tracking branch 'upstream/master' into number_field_type…
fred84 d76e214
fix checkstyle errors & return more informative message for malformed…
fred84 376a934
Merge remote-tracking branch 'upstream/master' into number_field_type…
fred84 cfe9461
move toLong from AbstractXContentParser to Numbers
fred84 3d8521b
fix test
fred84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
|
@@ -42,6 +44,9 @@ public abstract class AbstractXContentParser implements XContentParser { | |
// and change any code that needs to apply an alternative policy. | ||
public static final boolean DEFAULT_NUMBER_COERCE_POLICY = true; | ||
|
||
private static final BigInteger MIN_LONG_VALUE = BigInteger.valueOf(Long.MIN_VALUE); | ||
private static final BigInteger MAX_LONG_VALUE = BigInteger.valueOf(Long.MAX_VALUE); | ||
|
||
private static void checkCoerceString(boolean coerce, Class<? extends Number> clazz) { | ||
if (!coerce) { | ||
//Need to throw type IllegalArgumentException as current catch logic in | ||
|
@@ -133,7 +138,14 @@ public short shortValue(boolean coerce) throws IOException { | |
Token token = currentToken(); | ||
if (token == Token.VALUE_STRING) { | ||
checkCoerceString(coerce, Short.class); | ||
return (short) Double.parseDouble(text()); | ||
|
||
double doubleValue = Double.parseDouble(text()); | ||
|
||
if (doubleValue < Short.MIN_VALUE || doubleValue > Short.MAX_VALUE) { | ||
throw new IllegalArgumentException("Value [" + text() + "] is out of range for a short"); | ||
} | ||
|
||
return (short) doubleValue; | ||
} | ||
short result = doShortValue(); | ||
ensureNumberConversion(coerce, result, Short.class); | ||
|
@@ -152,7 +164,13 @@ public int intValue(boolean coerce) throws IOException { | |
Token token = currentToken(); | ||
if (token == Token.VALUE_STRING) { | ||
checkCoerceString(coerce, Integer.class); | ||
return (int) Double.parseDouble(text()); | ||
double doubleValue = Double.parseDouble(text()); | ||
|
||
if (doubleValue < Integer.MIN_VALUE || doubleValue > Integer.MAX_VALUE) { | ||
throw new IllegalArgumentException("Value [" + text() + "] is out of range for an integer"); | ||
} | ||
|
||
return (int) doubleValue; | ||
} | ||
int result = doIntValue(); | ||
ensureNumberConversion(coerce, result, Integer.class); | ||
|
@@ -176,14 +194,31 @@ public long longValue(boolean coerce) throws IOException { | |
try { | ||
return Long.parseLong(stringValue); | ||
} catch (NumberFormatException e) { | ||
return (long) Double.parseDouble(stringValue); | ||
return preciseLongValue(stringValue, coerce); | ||
} | ||
} | ||
long result = doLongValue(); | ||
ensureNumberConversion(coerce, result, Long.class); | ||
return result; | ||
} | ||
|
||
public static long preciseLongValue(String stringValue, boolean coerce) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you want to use it from different classes, please move it to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||
final BigInteger bigIntegerValue; | ||
|
||
try { | ||
BigDecimal bigDecimalValue = new BigDecimal(stringValue); | ||
bigIntegerValue = coerce ? bigDecimalValue.toBigInteger() : bigDecimalValue.toBigIntegerExact(); | ||
} catch (ArithmeticException e) { | ||
throw new IllegalArgumentException("Value [" + stringValue + "] has a decimal part"); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException("For input string: \"" + stringValue + "\""); | ||
} | ||
if (bigIntegerValue.compareTo(MAX_LONG_VALUE) > 0 || bigIntegerValue.compareTo(MIN_LONG_VALUE) < 0) { | ||
throw new IllegalArgumentException("Value [" + stringValue + "] is out of range for a long"); | ||
} | ||
return bigIntegerValue.longValue(); | ||
} | ||
|
||
protected abstract long doLongValue() throws IOException; | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it work if we only had
checkCoerceString
within this block? From what I read,doShortValue
already has the ability to parse strings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doShortValue
callsparser.getShortValue()
which throws exception on non-numeric json values. Even if it is number in wrapped in string: