Skip to content

LANG-1834 Fix Fraction reduction for Integer.MIN_VALUE - #1794

Merged
garydgregory merged 1 commit into
apache:masterfrom
makarandhinge:master
Sep 18, 2026
Merged

garydgregory merged 1 commit into
apache:masterfrom
makarandhinge:master

Conversation

@makarandhinge

Copy link
Copy Markdown
Contributor

Thanks for your contribution to Apache Commons! Your help is appreciated!

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all, of this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

Fix Fraction.getReducedFraction handling for Integer.MIN_VALUE with a negative denominator.

When the numerator is Integer.MIN_VALUE and the denominator is negative, the current implementation can attempt to negate Integer.MIN_VALUE, which overflows and throws an ArithmeticException even when the fraction can be reduced to a valid integer fraction.

This change reduces common powers of two from the numerator and denominator before sign normalization, avoiding the overflow for representable fractions while preserving overflow handling for results that cannot be represented as int.

Tests

Added regression coverage for:

  • Integer.MIN_VALUE / -2
  • Integer.MIN_VALUE / -6
  • Integer.MIN_VALUE / Integer.MIN_VALUE
  • Integer.MIN_VALUE / -1 (overflow)
  • Integer.MIN_VALUE / -3 (overflow)

All existing FractionTest tests continue to pass.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reviewed changes address the overflow case with regression coverage.

Pull request overview

Fixes Fraction.getReducedFraction handling for Integer.MIN_VALUE with negative denominators.

Changes:

  • Reduces common powers of two before sign normalization.
  • Adds regression tests for representable and overflowing edge cases.
File summaries
File Summary
src/test/java/org/apache/commons/lang3/math/FractionTest.java Adds boundary-value regression coverage.
src/main/java/org/apache/commons/lang3/math/Fraction.java Updates fraction reduction logic to avoid overflow.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@garydgregory
garydgregory merged commit e992a49 into apache:master Sep 18, 2026
23 of 24 checks passed
@garydgregory

Copy link
Copy Markdown
Member

@makarandhinge Thank you, merged 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants