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 85ec85ab1 Fix premature overflow in Fraction.divideBy
85ec85ab1 is described below
commit 85ec85ab1a223735e39c490bd6c5e498f4e2011d
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Nov 15 07:38:08 2026 -0500
Fix premature overflow in Fraction.divideBy
Follow up to PR #1865.
Clarify Fraction overflow contracts and expand division tests
Document reduced division results, numerator range limits, and the
Integer.MIN_VALUE overflow restriction in invert().
Add 10,816 parameterized division cases checked against BigInteger,
covering cancellation, signs, zero, and overflow. Verify that invert()
rejects an unreduced minimum-value numerator even when its reduced
reciprocal would be representable.
Assisted-By: OpenAI Codex
---
src/changes/changes.xml | 1 +
.../org/apache/commons/lang3/math/Fraction.java | 9 ++--
.../apache/commons/lang3/math/FractionTest.java | 56 ++++++++++++++++++++++
3 files changed, 63 insertions(+), 3 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 8592f3efc..24c99ddfb 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,6 +50,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary
Gregory">Javadoc fixes: DiffBuilder, ReflectionDiffBuilder,
TimedSemaphore.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory,
gaurav kumar pandey">Fix null parameter-type array handling in MethodUtils and
ConstructorUtils lookups, including varargs matching and overload
comparison.</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory,
Amar Ghugare">[LANG-1425] Clarify DateUtils millisecond parsing
(#1871).</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory,
Amar Ghugare">Fix premature overflow in Fraction.divideBy (#1865).</action>
<!-- ADD -->
<!-- UPDATE -->
</release>
diff --git a/src/main/java/org/apache/commons/lang3/math/Fraction.java
b/src/main/java/org/apache/commons/lang3/math/Fraction.java
index 0d7ebbe40..30f3bfac1 100644
--- a/src/main/java/org/apache/commons/lang3/math/Fraction.java
+++ b/src/main/java/org/apache/commons/lang3/math/Fraction.java
@@ -619,13 +619,14 @@ public int compareTo(final Fraction other) {
}
/**
- * Divide the value of this fraction by another.
+ * Divides the value of this fraction by another, returning the result in
reduced form.
*
* @param fraction The fraction to divide by, must not be {@code null}
* @return A {@link Fraction} instance with the resulting values
* @throws NullPointerException Thrown if the fraction is {@code null}.
* @throws ArithmeticException Thrown if the fraction to divide by is zero.
- * @throws ArithmeticException Thrown if the resulting numerator or
denominator exceeds {@code Integer.MAX_VALUE}.
+ * @throws ArithmeticException Thrown if the reduced numerator is outside
the range {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE},
+ * or the reduced denominator exceeds {@link Integer#MAX_VALUE}.
*/
public Fraction divideBy(final Fraction fraction) {
Objects.requireNonNull(fraction, "fraction");
@@ -777,6 +778,8 @@ public int intValue() {
*
* @return A new fraction instance with the numerator and denominator
inverted.
* @throws ArithmeticException Thrown if the fraction represents zero.
+ * @throws ArithmeticException Thrown if the numerator is {@link
Integer#MIN_VALUE}, because the inverted denominator cannot be represented
+ * as a positive {@code int}.
*/
public Fraction invert() {
if (numerator == 0) {
@@ -897,7 +900,7 @@ private void readObject(final ObjectInputStream in) throws
IOException, ClassNot
}
/**
- * Reduce the fraction to the smallest values for the numerator and
denominator, returning the result.
+ * Reduces the fraction to the smallest values for the numerator and
denominator, returning the result.
* <p>
* For example, if this fraction represents 2/4, then the result will be
1/2.
* </p>
diff --git a/src/test/java/org/apache/commons/lang3/math/FractionTest.java
b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
index e9c917623..7e63f9df4 100644
--- a/src/test/java/org/apache/commons/lang3/math/FractionTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/FractionTest.java
@@ -25,10 +25,17 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Stream;
+
import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests {@link Fraction}.
@@ -37,6 +44,23 @@ class FractionTest extends AbstractLangTest {
private static final int SKIP = 500; // 53
+ private static Stream<Arguments> divideBoundaryValues() {
+ final int[] numerators = {
+ Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -1073741824, -46341, -2,
-1, 0, 1, 2, 46341, 1073741824,
+ Integer.MAX_VALUE - 1, Integer.MAX_VALUE
+ };
+ final int[] denominators = {1, 2, 3, 4, 46341, 1073741824,
Integer.MAX_VALUE - 1, Integer.MAX_VALUE};
+ final List<Fraction> fractions = new ArrayList<>();
+ for (final int numerator : numerators) {
+ for (final int denominator : denominators) {
+ // Preserve unreduced operands so cancellation within each
operand is exercised as well.
+ fractions.add(Fraction.getFraction(numerator, denominator));
+ }
+ }
+ return fractions.stream()
+ .flatMap(dividend -> fractions.stream().map(divisor ->
Arguments.of(dividend, divisor)));
+ }
+
@Test
void testAbs() {
Fraction f;
@@ -396,6 +420,36 @@ void testDivide() {
assertEquals(463410000, f.getDenominator());
}
+ @ParameterizedTest(name = "[{index}] {0} / {1}")
+ @MethodSource("divideBoundaryValues")
+ void testDivideBoundaryValues(final Fraction dividend, final Fraction
divisor) {
+ if (divisor.getNumerator() == 0) {
+ assertThrows(ArithmeticException.class, () ->
dividend.divideBy(divisor));
+ return;
+ }
+
+ // Use arbitrary-precision arithmetic as an independent oracle for the
reduced quotient and its range.
+ final BigInteger numerator =
BigInteger.valueOf(dividend.getNumerator()).multiply(BigInteger.valueOf(divisor.getDenominator()));
+ final BigInteger denominator =
BigInteger.valueOf(dividend.getDenominator()).multiply(BigInteger.valueOf(divisor.getNumerator()));
+ final BigInteger signedGcd =
numerator.gcd(denominator).multiply(BigInteger.valueOf(denominator.signum()));
+ final BigInteger expectedNumerator = numerator.divide(signedGcd);
+ final BigInteger expectedDenominator = denominator.divide(signedGcd);
+ final BigInteger minValue = BigInteger.valueOf(Integer.MIN_VALUE);
+ final BigInteger maxValue = BigInteger.valueOf(Integer.MAX_VALUE);
+
+ if (expectedNumerator.compareTo(minValue) < 0 ||
expectedNumerator.compareTo(maxValue) > 0
+ || expectedDenominator.compareTo(maxValue) > 0) {
+ assertThrows(ArithmeticException.class, () ->
dividend.divideBy(divisor));
+ } else {
+ final Fraction quotient = dividend.divideBy(divisor);
+ assertEquals(expectedNumerator.intValueExact(),
quotient.getNumerator());
+ assertEquals(expectedDenominator.intValueExact(),
quotient.getDenominator());
+ if (expectedNumerator.signum() == 0) {
+ assertSame(Fraction.ZERO, quotient);
+ }
+ }
+ }
+
@ParameterizedTest
@CsvSource({
"-2147483648, 1, -2147483648, 1, 1, 1",
@@ -839,6 +893,8 @@ void testInvert() {
assertThrows(ArithmeticException.class, () -> Fraction.getFraction(0,
3).invert());
assertThrows(ArithmeticException.class, () ->
Fraction.getFraction(Integer.MIN_VALUE, 1).invert());
+ // Inversion does not reduce, even when the reduced reciprocal would
be representable.
+ assertThrows(ArithmeticException.class, () ->
Fraction.getFraction(Integer.MIN_VALUE, 2).invert());
f = Fraction.getFraction(Integer.MAX_VALUE, 1);
f = f.invert();