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

Fix problems from large array index #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arthurscchan
Copy link

This fixes a possible OutOfMemory problem and NegativeArraySizeException in diffutils/src/me/xdrop/diffutils/DiffUtils.java.

In the getEditOps() method of the DiffUtils class, the length of two strings (after removing matching suffix) are used to create the resulting matrix. The matrix is a 1D array with the size of the multiply of the two string lengths. There are two possible ways that this could go wrong if the two string length is too large. Firstly, if the multiplication result exceeds the Integer.MAX_VALUE of java, it will wrap around and stored as a negative number. This makes the creation of the matrix array throws a NegativeArraySizeException. In other cases, if the multiplication result does not exceed the Integer.MAX_VALUE of java but exceed the remaining size of the heap storage, it will cause an OutOfMemoryError. Sometimes, it does not need to happen immediately because there may be enough memory to create the big matrix but not enough to allocate for other variables in later operations.

This PR suggests to fix the problem by setting a class variable to define the maximum matrix size allowed. If the multiplication result is larger than the limit or is negative, then just throw an IllegalArgumentException to indicate the two strings are too long to process.

We found this bug using fuzzing by way of OSS-Fuzz, where we recently integrated fuzzywuzzy (google/oss-fuzz#10744). 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.

Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
@arthurscchan arthurscchan changed the title Fix possible large array index Fix problems from large array index Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants