JDK-6334849 : Unchecked scale manipulation in BigDecimal.dropDigits
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 5.0u5
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: other
  • Submitted: 2005-10-10
  • Updated: 2010-04-02
  • Resolved: 2005-12-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u8Fixed 6 b63Fixed
Description
OPERATING SYSTEM(S)
Windows XP
FULL JDK VERSION(S):
java version "1.5.0_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05)
Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode)
DESCRIPTION:
In some cases, rounding can cause an unnoticed overflow of a BigDecimal's scale.
This is caused by an unchecked manipulation of the scale in BigDecimal.dropDigits:
- Minimal source code that demonstrates the problem
import java.math.*;
public class BDScaleOverFlow {
    public static void main(String[] args) {
        BigDecimal j = BigDecimal.valueOf(11, Integer.MIN_VALUE);
        System.out.format("%s (scale: %s)\n", j, j.scale());
        //The following should throw an exception since we would be 
        //forcing the scale to fall below Integer.MIN_VALUE
        System.out.println("About to round to precision 1, expecting exception.");
        j = j.round(new MathContext(1));
        System.out.format("%s   (scale:  %s)", j, j.scale());
    }
}
- Exact text of any error messages
Output:
1.1E+2147483649 (scale: -2147483648)
About to round to precision 1, expecting exception.
1E-2147483647   (scale:  2147483647)
The testcase should throw an exception.

Comments
SUGGESTED FIX ------- BigDecimal.java ------- 3412c3414 < rounded.scale -= drop; // adjust the scale --- > rounded.scale = checkScale((long)rounded.scale - drop ); // adjust the scale
28-11-2005

EVALUATION Should be fixed; even extreme values should work properly.
25-10-2005

WORK AROUND In BigDecimal.dropDigits change: rounded.scale -= drop; // adjust the scale to: rounded.scale = checkScale((long)rounded.scale - drop); // adjust the scale with check
10-10-2005