diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 879c491ce..c155b39c3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + StringUtils.getLevenshteinDistance with too big of a threshold returns wrong result Test DurationFormatUtilsTest.testEdgeDuration fails in JDK 1.6, 1.7 and 1.8, BRST time zone ConstructorUtils.getAccessibleConstructor() Does Not Check the Accessibility of Enclosing Classes Fragments are wrong by 1 day when using fragment YEAR or MONTH diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 2a1c2f843..68bb78a2e 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -6938,7 +6938,7 @@ distance is O(nm), but a bound of k allows us to reduce it to O(km) time by only // compute stripe indices, constrain to array size final int min = Math.max(1, j - threshold); - final int max = Math.min(n, j + threshold); + final int max = (j > Integer.MAX_VALUE - threshold) ? n : Math.min(n, j + threshold); // the stripe may lead off of the table if s and t are of different sizes if (min > max) { diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index b4fda09ff..06e707958 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1974,6 +1974,14 @@ public void testGetLevenshteinDistance_StringStringInt() { assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo",8) ); assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo",1) ); + assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog", Integer.MAX_VALUE) ); + assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant", Integer.MAX_VALUE) ); + assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo", Integer.MAX_VALUE) ); + assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant", Integer.MAX_VALUE) ); + assertEquals(8, StringUtils.getLevenshteinDistance("hippo", "zzzzzzzz", Integer.MAX_VALUE) ); + assertEquals(8, StringUtils.getLevenshteinDistance("zzzzzzzz", "hippo", Integer.MAX_VALUE) ); + assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo", Integer.MAX_VALUE) ); + // exceptions try { @SuppressWarnings("unused")