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 d1611cac3 Fix integer overflow in StringUtils.join range handling
d1611cac3 is described below

commit d1611cac3059a4fe079be91d1f041afe340b209b
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 15 14:30:46 2026 -0500

    Fix integer overflow in StringUtils.join range handling
    
    Compare indices before subtracting in primitive-array and List
    overloads,
    and use long arithmetic for the object-array stream limit. Preserve
    validation order and clarify range behavior in Javadoc.
    
    Add regression tests for extreme end indices across all affected
    overloads
    and extreme negative start indices in both List overloads.
    
    Assisted-By: OpenAI Codex
---
 .../java/org/apache/commons/lang3/StringUtils.java | 138 ++++++++++-----------
 .../org/apache/commons/lang3/StringUtilsTest.java  |  29 +++++
 2 files changed, 96 insertions(+), 71 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index fe232fdc0..f03949b8f 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -3954,12 +3954,11 @@ public static String join(final boolean[] array, final 
char delimiter) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.12.0
      */
     public static String join(final boolean[] array, final char delimiter, 
final int startIndex, final int endIndex) {
@@ -3968,10 +3967,10 @@ public static String join(final boolean[] array, final 
char delimiter, final int
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 5; // "false"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4032,12 +4031,11 @@ public static String join(final byte[] array, final 
char delimiter) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final byte[] array, final char delimiter, final 
int startIndex, final int endIndex) {
@@ -4046,10 +4044,10 @@ public static String join(final byte[] array, final 
char delimiter, final int st
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 4; // "-128"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4110,12 +4108,11 @@ public static String join(final char[] array, final 
char delimiter) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final char[] array, final char delimiter, final 
int startIndex, final int endIndex) {
@@ -4124,10 +4121,10 @@ public static String join(final char[] array, final 
char delimiter, final int st
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 1;
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4188,12 +4185,11 @@ public static String join(final double[] array, final 
char delimiter) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final double[] array, final char delimiter, 
final int startIndex, final int endIndex) {
@@ -4202,10 +4198,10 @@ public static String join(final double[] array, final 
char delimiter, final int
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 22; // "1.7976931348623157E308"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4266,12 +4262,11 @@ public static String join(final float[] array, final 
char delimiter) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final float[] array, final char delimiter, final 
int startIndex, final int endIndex) {
@@ -4280,10 +4275,10 @@ public static String join(final float[] array, final 
char delimiter, final int s
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 12; // "3.4028235E38"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4344,12 +4339,11 @@ public static String join(final int[] array, final char 
separator) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final int[] array, final char delimiter, final 
int startIndex, final int endIndex) {
@@ -4358,10 +4352,10 @@ public static String join(final int[] array, final char 
delimiter, final int sta
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 11; // "-2147483648"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4482,17 +4476,18 @@ public static String join(final Iterator<?> iterator, 
final String separator) {
      *
      * @param list       The {@link List} of values to join together, may be 
null.
      * @param separator  The separator character to use.
-     * @param startIndex The first index to start joining from. It is an error 
to pass in a start index past the end of the list.
-     * @param endIndex   The index to stop joining from (exclusive). It is an 
error to pass in an end index past the end of the list.
-     * @return The joined String, {@code null} if null list input.
+     * @param startIndex The first index to start joining from.
+     * @param endIndex   The index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null list input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws IndexOutOfBoundsException if the list is non-null, {@code 
endIndex > startIndex}, and
+     *         either {@code startIndex < 0} or {@code endIndex > list.size()}.
      * @since 3.8
      */
     public static String join(final List<?> list, final char separator, final 
int startIndex, final int endIndex) {
         if (list == null) {
             return null;
         }
-        final int noOfItems = endIndex - startIndex;
-        if (noOfItems <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
         final List<?> subList = list.subList(startIndex, endIndex);
@@ -4517,17 +4512,18 @@ public static String join(final List<?> list, final 
char separator, final int st
      *
      * @param list       The {@link List} of values to join together, may be 
null.
      * @param separator  The separator character to use.
-     * @param startIndex The first index to start joining from. It is an error 
to pass in a start index past the end of the list.
-     * @param endIndex   The index to stop joining from (exclusive). It is an 
error to pass in an end index past the end of the list.
-     * @return The joined String, {@code null} if null list input.
+     * @param startIndex The first index to start joining from.
+     * @param endIndex   The index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null list input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws IndexOutOfBoundsException if the list is non-null, {@code 
endIndex > startIndex}, and
+     *         either {@code startIndex < 0} or {@code endIndex > list.size()}.
      * @since 3.8
      */
     public static String join(final List<?> list, final String separator, 
final int startIndex, final int endIndex) {
         if (list == null) {
             return null;
         }
-        final int noOfItems = endIndex - startIndex;
-        if (noOfItems <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
         final List<?> subList = list.subList(startIndex, endIndex);
@@ -4585,12 +4581,11 @@ public static String join(final long[] array, final 
char separator) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final long[] array, final char delimiter, final 
int startIndex, final int endIndex) {
@@ -4599,10 +4594,10 @@ public static String join(final long[] array, final 
char delimiter, final int st
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 20; // "-9223372036854775808"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
@@ -4658,9 +4653,11 @@ public static String join(final Object[] array, final 
char delimiter) {
      *
      * @param array      The array of values to join together, may be null.
      * @param delimiter  The separator character to use.
-     * @param startIndex The first index to start joining from. It is an error 
to pass in a start index past the end of the array.
-     * @param endIndex   The index to stop joining from (exclusive). It is an 
error to pass in an end index past the end of the array.
-     * @return The joined String, {@code null} if null array input.
+     * @param startIndex The first index to start joining from.
+     * @param endIndex   The index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     *         Otherwise, at most {@code endIndex - startIndex} entries are 
joined, limited by the number of entries remaining in the array.
+     * @throws IllegalArgumentException if the array is non-null and {@code 
startIndex < 0}.
      * @since 2.0
      */
     public static String join(final Object[] array, final char delimiter, 
final int startIndex, final int endIndex) {
@@ -4718,12 +4715,12 @@ public static String join(final Object[] array, final 
String delimiter) {
      * @param delimiter  The separator character to use, null treated as "".
      * @param startIndex The first index to start joining from.
      * @param endIndex   The index to stop joining from (exclusive).
-     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex - startIndex <= 0}.
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
      *         Otherwise, at most {@code endIndex - startIndex} entries are 
joined, limited by the number of entries remaining in the array.
      * @throws IllegalArgumentException if the array is non-null and {@code 
startIndex < 0}.
      */
     public static String join(final Object[] array, final String delimiter, 
final int startIndex, final int endIndex) {
-        return array != null ? 
Streams.of(array).skip(startIndex).limit(Math.max(0, endIndex - startIndex))
+        return array != null ? 
Streams.of(array).skip(startIndex).limit(Math.max(0L, (long) endIndex - 
startIndex))
                 
.collect(LangCollectors.joining(ObjectUtils.toString(delimiter), EMPTY, EMPTY, 
ObjectUtils::toString)) : null;
     }
 
@@ -4778,12 +4775,11 @@ public static String join(final short[] array, final 
char delimiter) {
      * @param delimiter
      *            the separator character to use.
      * @param startIndex
-     *            the first index to start joining from. It is an error to 
pass in a start index past the end of the
-     *            array.
+     *            the first index to start joining from.
      * @param endIndex
-     *            the index to stop joining from (exclusive). It is an error 
to pass in an end index past the end of
-     *            the array.
-     * @return The joined String, {@code null} if null array input.
+     *            the index to stop joining from (exclusive).
+     * @return The joined String, {@code null} if null array input; or the 
empty string if {@code endIndex <= startIndex}.
+     * @throws ArrayIndexOutOfBoundsException if the array is non-null and 
either {@code startIndex < 0} or {@code endIndex > array.length}.
      * @since 3.2
      */
     public static String join(final short[] array, final char delimiter, final 
int startIndex, final int endIndex) {
@@ -4792,10 +4788,10 @@ public static String join(final short[] array, final 
char delimiter, final int s
             return null;
         }
         checkFromToIndex(startIndex, endIndex, array.length);
-        final int count = endIndex - startIndex;
-        if (count <= 0) {
+        if (endIndex <= startIndex) {
             return EMPTY;
         }
+        final int count = endIndex - startIndex;
         final byte maxElementChars = 6; // "-32768"
         final StringBuilder stringBuilder = capacity(count, maxElementChars);
         stringBuilder.append(array[startIndex]);
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index fc50f91e9..37bc9977f 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1201,6 +1201,35 @@ 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, ','));

Reply via email to