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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion diffutils/src/me/xdrop/diffutils/DiffUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* so it is mostly non readable (eg. var names)
*/
public class DiffUtils {
/* Set the maximum size of the integer array for storing the
matrix to avoid filling up the heap and result in OOM */
public static int MAX_MATRIX_SIZE = 100000;

public static EditOp[] getEditOps(String s1, String s2) {
return getEditOps(s1.length(), s1, s2.length(), s2);
Expand Down Expand Up @@ -58,7 +61,16 @@ private static EditOp[] getEditOps(int len1, String s1, int len2, String s2) {
len1++;
len2++;

matrix = new int[len2 * len1];
/* Special checking to avoid creating a very large integer array
which will up the heap and cause OOM. It also avoid possible
negative index because of integer wrap around on the result
of large number multiplication. */
int matrixSize = len2 * len1;
if ((matrixSize > DiffUtils.MAX_MATRIX_SIZE) || (matrixSize <= 0)) {
throw new IllegalArgumentException("Provided strings are too long to handle.");
}

matrix = new int[matrixSize];

for (i = 0; i < len2; i++)
matrix[i] = i;
Expand Down