6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException
authorbpb
Mon, 24 Jun 2013 14:17:14 -0700
changeset 18545 5f4e734fad1b
parent 18544 54c23f2cc3a5
child 18546 862067c6481c
6469160: (fmt) general (%g) formatting of zero (0.0) with precision 0 or 1 throws ArrayOutOfBoundsException Summary: For zero value ensure than an unpadded zero character is passed to Formatter.addZeros() Reviewed-by: iris, darcy Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>
jdk/src/share/classes/java/util/Formatter.java
jdk/src/share/classes/sun/misc/FloatingDecimal.java
jdk/test/java/util/Formatter/Basic-X.java.template
jdk/test/java/util/Formatter/Basic.java
jdk/test/java/util/Formatter/BasicBigDecimal.java
jdk/test/java/util/Formatter/BasicDouble.java
jdk/test/java/util/Formatter/BasicDoubleObject.java
jdk/test/java/util/Formatter/BasicFloat.java
jdk/test/java/util/Formatter/BasicFloatObject.java
--- a/jdk/src/share/classes/java/util/Formatter.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/src/share/classes/java/util/Formatter.java	Mon Jun 24 14:17:14 2013 -0700
@@ -3297,18 +3297,29 @@
                 else if (precision == 0)
                     prec = 1;
 
-                FormattedFloatingDecimal fd
+                char[] exp;
+                char[] mant;
+                int expRounded;
+                if (value == 0.0) {
+                    exp = null;
+                    mant = new char[] {'0'};
+                    expRounded = 0;
+                } else {
+                    FormattedFloatingDecimal fd
                         = FormattedFloatingDecimal.valueOf(value, prec,
                           FormattedFloatingDecimal.Form.GENERAL);
-
-                char[] exp = fd.getExponent();
+                    exp = fd.getExponent();
+                    mant = fd.getMantissa();
+                    expRounded = fd.getExponentRounded();
+                }
+
                 if (exp != null) {
                     prec -= 1;
                 } else {
-                    prec = prec - (value == 0 ? 0 : fd.getExponentRounded()) - 1;
+                    prec -= expRounded + 1;
                 }
 
-                char[] mant = addZeros(fd.getMantissa(), prec);
+                mant = addZeros(mant, prec);
                 // If the precision is zero and the '#' flag is set, add the
                 // requested decimal point.
                 if (f.contains(Flags.ALTERNATE) && (prec == 0))
--- a/jdk/src/share/classes/sun/misc/FloatingDecimal.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/src/share/classes/sun/misc/FloatingDecimal.java	Mon Jun 24 14:17:14 2013 -0700
@@ -1045,7 +1045,7 @@
             this.nDigits = n;
         }
 
-        /*
+        /**
          * Takes a FloatingDecimal, which we presumably just scanned in,
          * and finds out what its value is, as a double.
          *
--- a/jdk/test/java/util/Formatter/Basic-X.java.template	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/Basic-X.java.template	Mon Jun 24 14:17:14 2013 -0700
@@ -1177,6 +1177,13 @@
         test("%.3G", "1.00E-05", recip(create(100000.0)));
         test("%.3G", "-1.00E-05", recip(create(-100000.0)));
 
+        test("%.1g", "-0", -0.0);
+        test("%3.0g", " -0", -0.0);
+        test("%.1g", "0", 0.0);
+        test("%3.0g", "  0", 0.0);
+        test("%.1g", "0", +0.0);
+        test("%3.0g", "  0", +0.0);
+
         test("%3.0g", "1e-06", 0.000001);
         test("%3.0g", "1e-05", 0.00001);
         test("%3.0g", "1e-05", 0.0000099);
--- a/jdk/test/java/util/Formatter/Basic.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/Basic.java	Mon Jun 24 14:17:14 2013 -0700
@@ -25,7 +25,7 @@
  * @summary Unit test for formatter
  * @bug 4906370 4962433 4973103 4989961 5005818 5031150 4970931 4989491 5002937
  *      5005104 5007745 5061412 5055180 5066788 5088703 6317248 6318369 6320122
- *      6344623 6369500 6534606 6282094 6286592 6476425 5063507
+ *      6344623 6369500 6534606 6282094 6286592 6476425 5063507 6469160
  *
  * @run shell/timeout=240 Basic.sh
  */
--- a/jdk/test/java/util/Formatter/BasicBigDecimal.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/BasicBigDecimal.java	Mon Jun 24 14:17:14 2013 -0700
@@ -1177,6 +1177,13 @@
         test("%.3G", "1.00E-05", recip(create(100000.0)));
         test("%.3G", "-1.00E-05", recip(create(-100000.0)));
 
+        test("%.1g", "-0", -0.0);
+        test("%3.0g", " -0", -0.0);
+        test("%.1g", "0", 0.0);
+        test("%3.0g", "  0", 0.0);
+        test("%.1g", "0", +0.0);
+        test("%3.0g", "  0", +0.0);
+
         test("%3.0g", "1e-06", 0.000001);
         test("%3.0g", "1e-05", 0.00001);
         test("%3.0g", "1e-05", 0.0000099);
--- a/jdk/test/java/util/Formatter/BasicDouble.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/BasicDouble.java	Mon Jun 24 14:17:14 2013 -0700
@@ -1123,6 +1123,15 @@
 
 
 
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %f - float, double, Double, BigDecimal
         //---------------------------------------------------------------------
@@ -1168,6 +1177,13 @@
         test("%.3G", "1.00E-05", recip(create(100000.0)));
         test("%.3G", "-1.00E-05", recip(create(-100000.0)));
 
+        test("%.1g", "-0", -0.0);
+        test("%3.0g", " -0", -0.0);
+        test("%.1g", "0", 0.0);
+        test("%3.0g", "  0", 0.0);
+        test("%.1g", "0", +0.0);
+        test("%3.0g", "  0", +0.0);
+
         test("%3.0g", "1e-06", 0.000001);
         test("%3.0g", "1e-05", 0.00001);
         test("%3.0g", "1e-05", 0.0000099);
--- a/jdk/test/java/util/Formatter/BasicDoubleObject.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/BasicDoubleObject.java	Mon Jun 24 14:17:14 2013 -0700
@@ -1123,6 +1123,15 @@
 
 
 
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %f - float, double, Double, BigDecimal
         //---------------------------------------------------------------------
@@ -1168,6 +1177,13 @@
         test("%.3G", "1.00E-05", recip(create(100000.0)));
         test("%.3G", "-1.00E-05", recip(create(-100000.0)));
 
+        test("%.1g", "-0", -0.0);
+        test("%3.0g", " -0", -0.0);
+        test("%.1g", "0", 0.0);
+        test("%3.0g", "  0", 0.0);
+        test("%.1g", "0", +0.0);
+        test("%3.0g", "  0", +0.0);
+
         test("%3.0g", "1e-06", 0.000001);
         test("%3.0g", "1e-05", 0.00001);
         test("%3.0g", "1e-05", 0.0000099);
--- a/jdk/test/java/util/Formatter/BasicFloat.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/BasicFloat.java	Mon Jun 24 14:17:14 2013 -0700
@@ -1106,6 +1106,15 @@
 
 
 
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %f - float
         //---------------------------------------------------------------------
@@ -1168,6 +1177,13 @@
         test("%.3G", "1.00E-05", recip(create(100000.0)));
         test("%.3G", "-1.00E-05", recip(create(-100000.0)));
 
+        test("%.1g", "-0", -0.0);
+        test("%3.0g", " -0", -0.0);
+        test("%.1g", "0", 0.0);
+        test("%3.0g", "  0", 0.0);
+        test("%.1g", "0", +0.0);
+        test("%3.0g", "  0", +0.0);
+
         test("%3.0g", "1e-06", 0.000001);
         test("%3.0g", "1e-05", 0.00001);
         test("%3.0g", "1e-05", 0.0000099);
--- a/jdk/test/java/util/Formatter/BasicFloatObject.java	Mon Jun 24 16:21:32 2013 -0700
+++ b/jdk/test/java/util/Formatter/BasicFloatObject.java	Mon Jun 24 14:17:14 2013 -0700
@@ -1139,6 +1139,15 @@
 
 
 
+
+
+
+
+
+
+
+
+
         //---------------------------------------------------------------------
         // %g
         //
@@ -1168,6 +1177,13 @@
         test("%.3G", "1.00E-05", recip(create(100000.0)));
         test("%.3G", "-1.00E-05", recip(create(-100000.0)));
 
+        test("%.1g", "-0", -0.0);
+        test("%3.0g", " -0", -0.0);
+        test("%.1g", "0", 0.0);
+        test("%3.0g", "  0", 0.0);
+        test("%.1g", "0", +0.0);
+        test("%3.0g", "  0", +0.0);
+
         test("%3.0g", "1e-06", 0.000001);
         test("%3.0g", "1e-05", 0.00001);
         test("%3.0g", "1e-05", 0.0000099);