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 455c6c421 Fix capacity overflow in StringUtils.wrapIfMissing
455c6c421 is described below

commit 455c6c4217bab55ec133f993f199ad03b51b8006
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 15 14:52:39 2026 -0500

    Fix capacity overflow in StringUtils.wrapIfMissing
    
    Calculate capacity using long arithmetic and count only missing
    wrappers.
    Reuse the safe StringBuilder helper to avoid overflow when the result
    is representable.
    
    Add six opt-in boundary cases covering both missing sides without
    duplicating existing functional tests. Update the release notes.
    
    Assisted-By: OpenAI Codex
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/lang3/StringUtils.java |  3 +-
 .../commons/lang3/StringUtilsOverflowTest.java     | 27 ++++++++++
 .../org/apache/commons/lang3/StringUtilsTest.java  | 58 +++++++++++-----------
 4 files changed, 59 insertions(+), 30 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e717cbead..3ae97a59f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -91,6 +91,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="fix" dev="ggregory" due-to="Gary 
Gregory">Javadoc: Correct the null-marker example, null-marker handling, and 
minimum widths in StringUtils.abbreviate(String, String, int, int).</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
Amar Ghugare">Correct NumberUtils.createNumber(String) result types for 
negative hexadecimal minima: Integer.MIN_VALUE now returns Integer instead of 
Long, and Long.MIN_VALUE returns Long instead of BigInteger; numeric values are 
unchanged (#1877).</action>
     <action                   type="fix" dev="ggregory" due-to="Gary Gregory, 
simpleqt">Fix integer overflow in StringUtils.join range handling.</action>
+    <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>
     <!-- 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 f03949b8f..d7d0ad87f 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -9453,7 +9453,8 @@ public static String wrapIfMissing(final String str, 
final String wrapWith) {
         if (!wrapStart && !wrapEnd) {
             return str;
         }
-        final StringBuilder builder = new StringBuilder(str.length() + 
wrapWith.length() + wrapWith.length());
+        final long capacity = (long) str.length() + (wrapStart ? 
wrapWith.length() : 0) + (wrapEnd ? wrapWith.length() : 0);
+        final StringBuilder builder = newStringBuilder(capacity);
         if (wrapStart) {
             builder.append(wrapWith);
         }
diff --git 
a/src/test/java/org/apache/commons/lang3/StringUtilsOverflowTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsOverflowTest.java
index 10f5bcdb4..fc6fb1606 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsOverflowTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsOverflowTest.java
@@ -20,6 +20,7 @@
 import static org.junit.jupiter.api.Assertions.assertAll;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.lang.reflect.Method;
 import java.nio.charset.StandardCharsets;
@@ -28,6 +29,7 @@
 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;
 import org.junit.jupiter.params.provider.ValueSource;
 
 /**
@@ -35,6 +37,7 @@
  *
  * <p>
  * Enable the large-input tests with {@code -Dtest.stringutils.large.heap=true 
-DheapSize=-Xmx8g}.
+ * Use {@code -DheapSize=-Xmx12g} on Java 8.
  * </p>
  */
 class StringUtilsOverflowTest extends AbstractLangTest {
@@ -130,4 +133,28 @@ void testUnwrapLengthOverflow() {
         final String token = StringUtils.repeat('x', 1 << 30);
         assertSame(token, StringUtils.unwrap(token, token));
     }
+
+    /**
+     * Exercises both missing sides around the overflow boundary of the former 
estimate, {@code 3 * wrapperLength + 1}.
+     */
+    @ParameterizedTest
+    @CsvSource({
+        "715827881, false",
+        "715827881, true",
+        "715827882, false",
+        "715827882, true",
+        "715827883, false",
+        "715827883, true"
+    })
+    @EnabledIfSystemProperty(named = "test.stringutils.large.heap", matches = 
"true")
+    void testWrapIfMissingCapacityBoundaries(final int wrapperLength, final 
boolean missingStart) {
+        final String wrapper = new String(new byte[wrapperLength], 
StandardCharsets.ISO_8859_1);
+        final String input = missingStart ? "x" + wrapper : wrapper + "x";
+        final String result = StringUtils.wrapIfMissing(input, wrapper);
+        // Check the complete result without allocating another multi-gigabyte 
expected string.
+        assertEquals(2L * wrapperLength + 1, result.length());
+        assertTrue(result.startsWith(wrapper));
+        assertEquals('x', result.charAt(wrapperLength));
+        assertTrue(result.endsWith(wrapper));
+    }
 }
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 37bc9977f..bba103808 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1201,35 +1201,6 @@ void testJoin_ArrayString_NonEmptyDelimiter(final String 
delimiter) {
         assertEquals("", StringUtils.join(MIXED_TYPE_LIST, delimiter, 2, 1));
     }
 
-    @Test
-    void testJoinListRangeExtremeStartIndexOverflow() {
-        final List<String> list = Arrays.asList("a", "b", "c");
-        assertThrows(IndexOutOfBoundsException.class, () -> 
StringUtils.join(list, ';', Integer.MIN_VALUE, 0));
-        assertThrows(IndexOutOfBoundsException.class, () -> 
StringUtils.join(list, ";", Integer.MIN_VALUE, 0));
-    }
-
-    @Test
-    void testJoinRangeExtremeEndIndexOverflow() {
-        // endIndex - startIndex overflows an int for extreme end indexes; the 
range must still be empty.
-        final String[] array = { "a", "b", "c" };
-        assertEquals("", StringUtils.join(array, "--", 1, Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(array, '-', 1, Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(array, "--", 2, Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new boolean[] { true, false, true }, 
';', 1, Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new byte[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new char[] { 'a', 'b', 'c' }, ';', 
1, Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new double[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new float[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new int[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new long[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(new short[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(Arrays.asList(ARRAY_LIST), ';', 1, 
Integer.MIN_VALUE));
-        assertEquals("", StringUtils.join(Arrays.asList(ARRAY_LIST), ";", 1, 
Integer.MIN_VALUE));
-        // A non-overflowing reversed range is empty as documented, and valid 
ranges are unchanged.
-        assertEquals("", StringUtils.join(array, "--", 2, 1));
-        assertEquals("b--c", StringUtils.join(array, "--", 1, 3));
-    }
-
     @Test
     void testJoin_IterableChar() {
         assertNull(StringUtils.join((Iterable<?>) null, ','));
@@ -1350,6 +1321,35 @@ void testJoin_Objects() {
         assertNull(StringUtils.join((Object[]) null));
     }
 
+    @Test
+    void testJoinListRangeExtremeStartIndexOverflow() {
+        final List<String> list = Arrays.asList("a", "b", "c");
+        assertThrows(IndexOutOfBoundsException.class, () -> 
StringUtils.join(list, ';', Integer.MIN_VALUE, 0));
+        assertThrows(IndexOutOfBoundsException.class, () -> 
StringUtils.join(list, ";", Integer.MIN_VALUE, 0));
+    }
+
+    @Test
+    void testJoinRangeExtremeEndIndexOverflow() {
+        // endIndex - startIndex overflows an int for extreme end indexes; the 
range must still be empty.
+        final String[] array = { "a", "b", "c" };
+        assertEquals("", StringUtils.join(array, "--", 1, Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(array, '-', 1, Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(array, "--", 2, Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new boolean[] { true, false, true }, 
';', 1, Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new byte[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new char[] { 'a', 'b', 'c' }, ';', 
1, Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new double[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new float[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new int[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new long[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(new short[] { 1, 2, 3 }, ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(Arrays.asList(ARRAY_LIST), ';', 1, 
Integer.MIN_VALUE));
+        assertEquals("", StringUtils.join(Arrays.asList(ARRAY_LIST), ";", 1, 
Integer.MIN_VALUE));
+        // A non-overflowing reversed range is empty as documented, and valid 
ranges are unchanged.
+        assertEquals("", StringUtils.join(array, "--", 2, 1));
+        assertEquals("b--c", StringUtils.join(array, "--", 1, 3));
+    }
+
     @ParameterizedTest
     @ValueSource(strings = {",", ";", Supplementary.CharU20000, 
Supplementary.CharU20001})
     void testJoinWith(final String delimiter) {

Reply via email to