Skip to content
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

fixes OOM error with multiple decimal points in a number literal #413

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/main/java/com/ezylang/evalex/parser/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ private boolean implicitMultiplicationPossible(Token currentToken) {
}

private void validateToken(Token currentToken) throws ParseException {

if (currentToken.getType() == STRUCTURE_SEPARATOR && getPreviousToken() == null) {
throw new ParseException(currentToken, "Misplaced structure operator");
}

Token previousToken = getPreviousToken();
if (previousToken != null
&& previousToken.getType() == INFIX_OPERATOR
Expand Down Expand Up @@ -365,7 +370,17 @@ private Token parseDecimalNumberLiteral() throws ParseException {

int lastChar = -1;
boolean scientificNotation = false;
boolean dotEncountered = false;
while (currentChar != -1 && isAtNumberChar()) {
if (currentChar == '.' && dotEncountered) {
tokenValue.append((char) currentChar);
throw new ParseException(
new Token(tokenStartIndex, tokenValue.toString(), TokenType.NUMBER_LITERAL),
"Number contains more than one decimal point");
}
if (currentChar == '.') {
dotEncountered = true;
}
if (currentChar == 'e' || currentChar == 'E') {
scientificNotation = true;
}
Expand Down Expand Up @@ -510,7 +525,7 @@ private boolean isAtNumberChar() {
return Character.isDigit(currentChar) || currentChar == '+' || currentChar == '-';
}

if (previousChar == '.') {
if (previousChar == '.' && currentChar != '.') {
return Character.isDigit(currentChar) || currentChar == 'e' || currentChar == 'E';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ void testScientificLiteralsParseException(String expression) {
.isInstanceOf(ParseException.class)
.hasMessage("Illegal scientific format");
}

@ParameterizedTest
@ValueSource(strings = {"1..0*2.7*195.0", "123.45.6", "2.1.2..4", ".2.4"})
void testMoreThanOneDecimalPointThrowsException(String expression) {
assertThatThrownBy(() -> new Tokenizer(expression, configuration).parse())
.isInstanceOf(ParseException.class)
.hasMessage("Number contains more than one decimal point");
}

@Test
void testMisplacedStructureOperator() {
assertThatThrownBy(() -> new Tokenizer("..3", configuration).parse())
.isInstanceOf(ParseException.class)
.hasMessage("Misplaced structure operator");
}
}