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 1d1044848 Fix int overflow in FluentBitSet.setInclusive at
Integer.MAX_VALUE (#1751)
1d1044848 is described below
commit 1d1044848b38fa82063de9e33b9e54caaddfbf09
Author: alhuda <[email protected]>
AuthorDate: Sat Jul 18 01:39:54 2026 +0530
Fix int overflow in FluentBitSet.setInclusive at Integer.MAX_VALUE (#1751)
---
.../org/apache/commons/lang3/util/FluentBitSet.java | 8 +++++++-
.../org/apache/commons/lang3/util/FluentBitSetTest.java | 17 +++++++++++++++++
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java
b/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java
index 372076ae1..bb87c7e11 100644
--- a/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java
+++ b/src/main/java/org/apache/commons/lang3/util/FluentBitSet.java
@@ -517,7 +517,13 @@ public FluentBitSet set(final int fromIndex, final int
toIndex, final boolean va
* @return {@code this} instance.
*/
public FluentBitSet setInclusive(final int fromIndex, final int toIndex) {
- bitSet.set(fromIndex, toIndex + 1);
+ if (toIndex == Integer.MAX_VALUE) {
+ // toIndex + 1 would overflow to Integer.MIN_VALUE.
+ bitSet.set(fromIndex, toIndex);
+ bitSet.set(toIndex);
+ } else {
+ bitSet.set(fromIndex, toIndex + 1);
+ }
return this;
}
diff --git a/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java
b/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java
index cb94598a4..ac4a5b877 100644
--- a/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java
+++ b/src/test/java/org/apache/commons/lang3/util/FluentBitSetTest.java
@@ -32,6 +32,7 @@
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
/**
* Tests {@link FluentBitSet}.
@@ -1607,6 +1608,22 @@ void test_setInclusive() {
}
}
+ /**
+ * Tests {@link FluentBitSet#setInclusive(int, int)} at the {@link
Integer#MAX_VALUE} boundary.
+ * <p>
+ * Needs a large heap because bit {@link Integer#MAX_VALUE} forces the
backing array to full size.
+ * </p>
+ */
+ @Test
+ @EnabledIfSystemProperty(named = "test.large.heap", matches = "true")
+ void test_setInclusive_maxValue() {
+ final FluentBitSet bs = newInstance();
+ // toIndex == Integer.MAX_VALUE is a legal inclusive bound, not a
documented throw case.
+ bs.setInclusive(Integer.MAX_VALUE, Integer.MAX_VALUE);
+ assertTrue(bs.get(Integer.MAX_VALUE), "bit Integer.MAX_VALUE should be
set");
+ assertEquals(1, bs.cardinality());
+ }
+
/**
* Tests {@link FluentBitSet#set(int)}.
*/