From 8cd2339a9788cf2c6e44b6761d3a19259fbe1df3 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Wed, 22 Jan 2014 08:19:22 +0000 Subject: [PATCH] Applying Eli Lindsey's patch to Yaniv Kunda's report in LANG-936 that StringUtils.getLevensteinDistance(String, String, int) gave the wrong answer when the int threshold is near Integer.MAX_VALUE git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1560275 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/lang3/StringUtils.java | 2 +- .../java/org/apache/commons/lang3/StringUtilsTest.java | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) 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 @@ public class StringUtils { // 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 class StringUtilsTest { 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")