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.
This fixes an unwrapped ArrayIndexOutOfBoundException in src/main/java/net/objecthunter/exp4j/tokenizer/Tokenizer.java.
If the expression only contains whitespace characters, or if the expression is ended with whitespace characters. It will result in a possible ArrayIndexOutOfBoundException. This is because the while loop for skipping whitespace characters has incorrect stopping criteria. The while loop will keep running until reaching a non-whitespace character; if the remaining expression only contains whitespace, the loop will not stop at the end of the expression array and the ++pos index will be out of bound in the next iteration.
This PR adds a conditional checking to ensure an exception is thrown if nextToken method is called and only whitespace characters left in the expression to ensure the pos index used in the while loop will not be out of bound.
We found this bug using fuzzing by way of OSS-Fuzz, where we recently integrated exp4j (google/oss-fuzz#10699). OSS-Fuzz is a free service run by Google for fuzzing important open source software. If you'd like to know more about this then I'm happy to go into detail and also set up things so you can receive emails and detailed reports when bugs are found.