src/java.base/share/classes/sun/misc/FloatingDecimal.java

Print this page
rev 10699 : 8043740: Doubles with large exponents overflow to Infinity incorrectly
Summary: Modify test of exponent overflow to account for subsequent decrement.
Reviewed-by: TBD
Contributed-by: Sandipan Razzaque <me@sandipan.net>


1975                     //FALLTHROUGH
1976                 case '+':
1977                     i++;
1978                 }
1979                 int expAt = i;
1980             expLoop:
1981                 while ( i < len  ){
1982                     if ( expVal >= reallyBig ){
1983                         // the next character will cause integer
1984                         // overflow.
1985                         expOverflow = true;
1986                     }
1987                     c = in.charAt(i++);
1988                     if(c>='0' && c<='9') {
1989                         expVal = expVal*10 + ( (int)c - (int)'0' );
1990                     } else {
1991                         i--;           // back up.
1992                         break expLoop; // stop parsing exponent.
1993                     }
1994                 }
1995                 int expLimit = BIG_DECIMAL_EXPONENT+nDigits+nTrailZero;
1996                 if ( expOverflow || ( expVal > expLimit ) ){









1997                     //
1998                     // The intent here is to end up with
1999                     // infinity or zero, as appropriate.
2000                     // The reason for yielding such a small decExponent,
2001                     // rather than something intuitive such as
2002                     // expSign*Integer.MAX_VALUE, is that this value
2003                     // is subject to further manipulation in
2004                     // doubleValue() and floatValue(), and I don't want
2005                     // it to be able to cause overflow there!
2006                     // (The only way we can get into trouble here is for
2007                     // really outrageous nDigits+nTrailZero, such as 2 billion. )

2008                     //
2009                     decExp = expSign*expLimit;

2010                 } else {
2011                     // this should not overflow, since we tested
2012                     // for expVal > (MAX+N), where N >= abs(decExp)
2013                     decExp = decExp + expSign*expVal;
2014                 }
2015 
2016                 // if we saw something not a digit ( or end of string )
2017                 // after the [Ee][+-], without seeing any digits at all
2018                 // this is certainly an error. If we saw some digits,
2019                 // but then some trailing garbage, that might be ok.
2020                 // so we just fall through in that case.
2021                 // HUMBUG
2022                 if ( i == expAt ) {
2023                     break parseNumber; // certainly bad
2024                 }
2025             }
2026             //
2027             // We parsed everything we could.
2028             // If there are leftovers, then this is not good input!
2029             //




1975                     //FALLTHROUGH
1976                 case '+':
1977                     i++;
1978                 }
1979                 int expAt = i;
1980             expLoop:
1981                 while ( i < len  ){
1982                     if ( expVal >= reallyBig ){
1983                         // the next character will cause integer
1984                         // overflow.
1985                         expOverflow = true;
1986                     }
1987                     c = in.charAt(i++);
1988                     if(c>='0' && c<='9') {
1989                         expVal = expVal*10 + ( (int)c - (int)'0' );
1990                     } else {
1991                         i--;           // back up.
1992                         break expLoop; // stop parsing exponent.
1993                     }
1994                 }
1995                 int expLimit = BIG_DECIMAL_EXPONENT + nDigits + nTrailZero;
1996                 if (expOverflow || (expVal > expLimit)) {
1997                     // There is still a chance that the exponent will be safe to
1998                     // use: if it would eventually decrease due to a negative
1999                     // decExp, and that number is below the limit.  We check for
2000                     // that here.
2001                     if ((expSign == 1 && decExp < 0)
2002                             && (expVal + decExp) < expLimit) {
2003                         // Cannot overflow: adding a positive and negative number.
2004                         decExp += expVal;
2005                     } else {
2006                         //
2007                         // The intent here is to end up with
2008                         // infinity or zero, as appropriate.
2009                         // The reason for yielding such a small decExponent,
2010                         // rather than something intuitive such as
2011                         // expSign*Integer.MAX_VALUE, is that this value
2012                         // is subject to further manipulation in
2013                         // doubleValue() and floatValue(), and I don't want
2014                         // it to be able to cause overflow there!
2015                         // (The only way we can get into trouble here is for
2016                         // really outrageous nDigits+nTrailZero, such as 2
2017                         // billion.)
2018                         //
2019                         decExp = expSign * expLimit;
2020                     }
2021                 } else {
2022                     // this should not overflow, since we tested
2023                     // for expVal > (MAX+N), where N >= abs(decExp)
2024                     decExp = decExp + expSign*expVal;
2025                 }
2026 
2027                 // if we saw something not a digit ( or end of string )
2028                 // after the [Ee][+-], without seeing any digits at all
2029                 // this is certainly an error. If we saw some digits,
2030                 // but then some trailing garbage, that might be ok.
2031                 // so we just fall through in that case.
2032                 // HUMBUG
2033                 if ( i == expAt ) {
2034                     break parseNumber; // certainly bad
2035                 }
2036             }
2037             //
2038             // We parsed everything we could.
2039             // If there are leftovers, then this is not good input!
2040             //