[
https://issues.apache.org/jira/browse/LANG-1834?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Gary D. Gregory resolved LANG-1834.
-----------------------------------
Fix Version/s: 3.21.0
Resolution: Fixed
> raction.getReducedFraction incorrectly throws for reducible Integer.MIN_VALUE
> numerator
> ---------------------------------------------------------------------------------------
>
> Key: LANG-1834
> URL: https://issues.apache.org/jira/browse/LANG-1834
> Project: Commons Lang
> Issue Type: Bug
> Components: lang.*, lang.math.*
> Affects Versions: 3.20.0
> Environment: * Java: 17
> * Apache Commons Lang
> * Test framework: JUnit 5
> Reporter: 尹茂椿萱
> Priority: Major
> Fix For: 3.21.0
>
>
> {{Fraction.getReducedFraction(Integer.MIN_VALUE, -2)}} throws an
> {{{}ArithmeticException{}}}:
>
> {{ArithmeticException: overflow: can't negate}}
> However, the fraction can be safely reduced to {{{}1073741824/1{}}}, since
> both values are representable as {{{}int{}}}.
> The implementation already contains a historical special case for
> {{Integer.MIN_VALUE}} as the denominator:
>
> {{if (denominator == Integer.MIN_VALUE && (numerator & 1) == 0) \{
> numerator /= 2;
> denominator /= 2;
> }}}
> This modification handles some {{Integer.MIN_VALUE}} cases, but the handling
> is incomplete. It does not cover the case where {{Integer.MIN_VALUE}} is the
> numerator and the fraction can be reduced before sign normalization.
> For example:
>
> {{Fraction.getReducedFraction(Integer.MIN_VALUE, -2);}}
> should return a fraction with numerator {{1073741824}} and denominator
> {{{}1{}}}, but currently throws an exception.
> *Suggested fix:* reduce the fraction before negating
> {{{}Integer.MIN_VALUE{}}}, or otherwise handle reducible
> {{Integer.MIN_VALUE}} numerators before sign normalization.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)