This is an automated email from the ASF dual-hosted git repository.

garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 38f86239c Fix score overflow in StringUtils.getFuzzyDistance
38f86239c is described below

commit 38f86239cd4bebee931b1d01cea64d3083dc1971
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 15 16:25:46 2026 -0500

    Fix score overflow in StringUtils.getFuzzyDistance
    
    Accumulate scores in a long and use checked conversion to int,
    throwing ArithmeticException when the result exceeds Integer.MAX_VALUE.
    
    Add exhaustive small-input comparisons and opt-in score-boundary
    regressions. Update Javadoc and release notes.
    
    Assisted-By: OpenAI Codex
---
 src/changes/changes.xml                            |   1 +
 .../java/org/apache/commons/lang3/StringUtils.java |   5 +-
 .../lang3/StringUtilsFuzzyDistanceTest.java        | 153 +++++++++++++++++++++
 3 files changed, 157 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 32a4d7139..9d901de19 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -94,6 +94,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary 
Gregory">Prevent capacity overflow in StringUtils.wrapIfMissing(String, String) 
when only one wrapper is missing and the result is representable.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary 
Gregory">Prevent premature length overflow in StringUtils.repeat(String, 
String, int) when the final result is representable but the intermediate result 
with a trailing separator is not.</action>
     <action                   type="fix" dev="ggregory" due-to="Gary 
Gregory">Prevent loop-counter and edit-cost overflow in 
StringUtils.getLevenshteinDistance for CharSequence inputs of Integer.MAX_VALUE 
length, with and without a threshold.</action>
+    <action                   type="fix" dev="ggregory" due-to="Gary 
Gregory">Prevent score overflow in StringUtils.getFuzzyDistance(CharSequence, 
CharSequence, Locale), throwing ArithmeticException when the score exceeds 
Integer.MAX_VALUE.</action>
     <!-- ADD -->
     <!-- UPDATE -->
   </release>
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 3cddd2773..cf94148f0 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -2155,6 +2155,7 @@ public static String getDigits(final String str) {
      * @param locale This string matching logic is case-insensitive. A locale 
is necessary to normalize both Strings to lower case.
      * @return result score.
      * @throws IllegalArgumentException Thrown if either String input {@code 
null} or Locale input {@code null}.
+     * @throws ArithmeticException Thrown if the score exceeds {@link 
Integer#MAX_VALUE}.
      * @since 3.4
      * @deprecated As of 3.6, use Apache Commons Text
      *             <a 
href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/similarity/FuzzyScore.html";>
@@ -2175,7 +2176,7 @@ public static int getFuzzyDistance(final CharSequence 
term, final CharSequence q
         final String termLowerCase = term.toString().toLowerCase(locale);
         final String queryLowerCase = query.toString().toLowerCase(locale);
         // the resulting score
-        int score = 0;
+        long score = 0;
         // the position in the term which will be scanned next for potential
         // query character matches
         int termIndex = 0;
@@ -2201,7 +2202,7 @@ public static int getFuzzyDistance(final CharSequence 
term, final CharSequence q
                 }
             }
         }
-        return score;
+        return Math.toIntExact(score);
     }
 
     /**
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsFuzzyDistanceTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsFuzzyDistanceTest.java
new file mode 100644
index 000000000..01443393b
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsFuzzyDistanceTest.java
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Score-boundary and exhaustive small-input tests for the legacy fuzzy 
distance implementation.
+ *
+ * <p>
+ * Enable the large-input tests with {@code -Dtest.stringutils.large.heap=true 
-DheapSize=-Xmx8g}.
+ * Use {@code -DheapSize=-Xmx12g} on Java 8.
+ * </p>
+ */
+class StringUtilsFuzzyDistanceTest extends AbstractLangTest {
+
+    // Invoke the legacy method reflectively without introducing deprecated 
API usages.
+    private static int distance(final CharSequence term, final CharSequence 
query) throws ReflectiveOperationException {
+        final Method method = StringUtils.class.getMethod("getFuzzyDistance", 
CharSequence.class, CharSequence.class, Locale.class);
+        return (Integer) method.invoke(null, term, query, Locale.ROOT);
+    }
+
+    /**
+     * Counts greedy matches and their contiguous runs instead of 
incrementally accumulating the score.
+     * Each run contributes three points per character, minus the first 
character's missing two-point bonus.
+     */
+    private static int referenceDistance(final String term, final String 
query) {
+        int matches = 0;
+        int runs = 0;
+        int nextIndex = 0;
+        for (int i = 0; i < query.length(); i++) {
+            final int index = term.indexOf(query.charAt(i), nextIndex);
+            if (index < 0) {
+                break;
+            }
+            if (matches == 0 || index != nextIndex) {
+                runs++;
+            }
+            matches++;
+            nextIndex = index + 1;
+        }
+        return 3 * matches - 2 * runs;
+    }
+
+    private static String termWithGaps(final String query, final int 
initialGaps, final boolean lastGap) {
+        if (initialGaps == 0 && !lastGap) {
+            return query;
+        }
+        final char[] chars = new char[query.length() + initialGaps + (lastGap 
? 1 : 0)];
+        Arrays.fill(chars, 'a');
+        for (int i = 0; i < initialGaps; i++) {
+            chars[2 * i + 1] = 'b';
+        }
+        if (lastGap) {
+            chars[chars.length - 2] = 'b';
+        }
+        return new String(chars);
+    }
+
+    @Test
+    void testAllShortBinaryInputs() throws ReflectiveOperationException {
+        final List<String> inputs = new ArrayList<>();
+        for (int length = 1; length <= 5; length++) {
+            for (int bits = 0; bits < (1 << length); bits++) {
+                final StringBuilder text = new StringBuilder(length);
+                for (int bit = 0; bit < length; bit++) {
+                    text.append((bits & (1 << bit)) == 0 ? 'x' : 'y');
+                }
+                inputs.add(text.toString());
+            }
+        }
+        for (final String term : inputs) {
+            for (final String query : inputs) {
+                assertEquals(referenceDistance(term, query), distance(term, 
query), term + " -> " + query);
+            }
+        }
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "715827882, 0, false, 2147483644",
+        "715827883, 1, false, 2147483645",
+        "715827884, 2, false, 2147483646",
+        "715827883, 0, false, 2147483647",
+        "715827884, 1, true,  2147483646",
+        "715827885, 2, true,  2147483647"
+    })
+    @EnabledIfSystemProperty(named = "test.stringutils.large.heap", matches = 
"true")
+    void testRepresentableScoreBoundaries(final int matches, final int 
initialGaps, final boolean lastGap, final int expected)
+            throws ReflectiveOperationException {
+        // A run of n matches scores 3*n-2; each gap removes two bonus points.
+        // Include both consecutive and non-consecutive final matches at the 
integer limit.
+        final String query = StringUtils.repeat('a', matches);
+        assertEquals(expected, distance(termWithGaps(query, initialGaps, 
lastGap), query));
+    }
+
+    @Test
+    @EnabledIfSystemProperty(named = "test.stringutils.large.heap", matches = 
"true")
+    void testScoreAtMaximumWithUnmatchedSuffix() throws 
ReflectiveOperationException {
+        final String term = StringUtils.repeat('a', 715_827_883);
+        final String query = term + "b";
+        assertEquals(Integer.MAX_VALUE, distance(term, query));
+        assertEquals(Integer.MAX_VALUE, distance(query, term));
+    }
+
+    @ParameterizedTest
+    @CsvSource({
+        "715827884,  1, false",
+        "715827885,  2, false",
+        "715827884,  0, false",
+        "715827884,  0, true",
+        "1431655766, 0, false",
+        "1431655767, 0, false"
+    })
+    @EnabledIfSystemProperty(named = "test.stringutils.large.heap", matches = 
"true")
+    void testUnrepresentableScores(final int matches, final int initialGaps, 
final boolean lastGap) {
+        // Exercise scores MAX_VALUE+1, +2 and +3, an overflowing 
non-consecutive match,
+        // and scores that would wrap to zero or a positive int.
+        final String query = StringUtils.repeat('a', matches);
+        final String term = termWithGaps(query, initialGaps, lastGap);
+        final InvocationTargetException exception = 
assertThrows(InvocationTargetException.class, () -> distance(term, query));
+        assertInstanceOf(ArithmeticException.class, exception.getCause());
+    }
+}

Reply via email to