United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4707389 {Float, Double}.valueOf erroneously accepts integer strings
JDK-4707389 : {Float, Double}.valueOf erroneously accepts integer strings

Details
Type:
Bug
Submit Date:
2002-06-25
Status:
Resolved
Updated Date:
2003-04-10
Project Name:
JDK
Resolved Date:
2003-04-10
Component:
core-libs
OS:
solaris_2.5,generic,windows_2000
Sub-Component:
java.lang
CPU:
x86,sparc,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.1.4,1.2.0,1.4.0,1.4.2
Fixed Versions:
5.0 (tiger)

Related Reports
Duplicate:
Relates:

Sub Tasks

Description
The valueOf methods in Float and Double convert (essentially) strings of floating-point literals into floating-point values.  The set of accepted strings does *not* include simple integer strings like "45".  However, in clear violation of the specification, the current implementation accepts such strings instead of throwing a NumberFormatException.

                                    

Comments
EVALUATION

The code in FloatingDecimal which implements the valueOf functionality should test to make sure the required portions of the string are present.

###@###.### 2002-06-24

The specification has been changed to allow integer-only input.

###@###.### 2003-02-13
                                     
2002-06-24
SUGGESTED FIX

See suggested fix for bug 4705734 for JavaDoc changes associated with this bug.

###@###.### 2002-07-08

Specification changed to match implementation; changes listed below

 src/share/classes/java/lang/Float.java
=========================================

------- Float.java -------
146,152c146,156
<      * <p>
<      * If <code>s</code> is <code>null</code>, then a 
<      * <code>NullPointerException</code> is thrown. 
<      * <p>
<      * Leading and trailing whitespace characters in <code>s</code>
<      * are ignored. The rest of <code>s</code> should constitute a
<      * <i>FloatValue</i> as described by the lexical syntax rules:
---
>      * 
>      * <p>If <code>s</code> is <code>null</code>, then a
>      * <code>NullPointerException</code> is thrown.
>      * 
>      * <p>Leading and trailing whitespace characters in <code>s</code>
>      * are ignored.  Whitespace is removed as if by the {@link
>      * String#trim} method; that is, both ASCII space and control
>      * characters are removed. The rest of <code>s</code> should
>      * constitute a <i>FloatValue</i> as described by the lexical
>      * syntax rules:
>      *
158a163
>      * <dd>SignedInteger
161,163c166,170
<      * where <i>Sign</i> and <i>FloatingPointLiteral</i> are as
<      * defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#230798">&sect;3.10.2</a> 
<      * of the <a href="http://java.sun.com/docs/books/jls/html/">Java 
---
>      *
>      * where <i>Sign</i>, <i>FloatingPointLiteral</i>, and
>      * <i>SignedInteger</i> are as defined in <a
>      * href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#230798">&sect;3.10.2</a>
>      * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
175,176c182,183
<      * <p>
<      * To interpret localized string representations of a
---
>      * 
>      * <p>To interpret localized string representations of a
197a205,209
>      * <p>To avoid calling this method on a invalid string and having
>      * a <code>NumberFormatException</code> be thrown, the documentation
>      * for {@link Double#valueOf Double.valueOf} lists a regular
>      * expression which can be used to screen the input.
>      *


 src/share/classes/java/lang/Double.java
==========================================

------- Double.java -------
142,143c142,143
<      * <p>
<      * If <code>s</code> is <code>null</code>, then a 
---
>      * 
>      * <p>If <code>s</code> is <code>null</code>, then a 
145,148c145,152
<      * <p>
<      * Leading and trailing whitespace characters in <code>s</code>
<      * are ignored. The rest of <code>s</code> should constitute a
<      * <i>FloatValue</i> as described by the lexical rule:
---
>      *
>      * <p>Leading and trailing whitespace characters in <code>s</code>
>      * are ignored.  Whitespace is removed as if by the {@link
>      * String#trim} method; that is, both ASCII space and control
>      * characters are removed. The rest of <code>s</code> should
>      * constitute a <i>FloatValue</i> as described by the lexical
>      * syntax rules:
>      *
154a159
>      * <dd>SignedInteger
157,158c162,164
<      * where <i>Sign</i> and <i>FloatingPointLiteral</i> are as
<      * defined in 
---
>      *
>      * where <i>Sign</i>, <i>FloatingPointLiteral</i>, and
>      * <i>SignedInteger</i> are as defined in
161,166c167,173
<      * Language Specification</a>. If <code>s</code> does not have the 
<      * form of a <i>FloatValue</i>, then a <code>NumberFormatException</code>
<      * is thrown. Otherwise, <code>s</code> is regarded as
<      * representing an exact decimal value in the usual "computerized
<      * scientific notation"; this exact decimal value is then
<      * conceptually converted to an "infinitely precise" binary value
---
>      * Language Specification</a>. If <code>s</code> does not have the
>      * form of a <i>FloatValue</i>, then a
>      * <code>NumberFormatException</code> is thrown. Otherwise,
>      * <code>s</code> is regarded as representing an exact decimal
>      * value in the usual &quot;computerized scientific
>      * notation&quot;; this exact decimal value is then conceptually
>      * converted to an &quot;infinitely precise&quot; binary value
171,173c178,180
<      * <code>double</code> value is returned.
<      * <p>
<      * To interpret localized string representations of a
---
>      * <code>double</code> value is returned.  
>      *
>      * <p> To interpret localized string representations of a
194a202,244
>      * <p>To avoid calling this method on a invalid string and having
>      * a <code>NumberFormatException</code> be thrown, the regular
>      * expression below can be used to screen the input string:
>      *
>      * <code>
>      * <pre>
>      *        final String Digits     = "(\\p{Digit}+)";
>      *        // an exponent is 'e' or 'E' followed by an optionally 
>      *        // signed decimal integer.
>      *        final String Exp        = "[eE][+-]?"+Digits;
>      *        final String fpRegex    =
>      *            ("[\\x00-\\x20]*"+  // Optional leading &quot;whitespace&quot;
>      *             "[+-]?(" + // Optional sign character
>      *             "NaN|" +           // "NaN" string
>      *             "Infinity|" +      // "Infinity" string
>      *
>      *             // A floating-point string representing a finite positive
>      *             // number without a leading sign has at most five basic pieces:
>      *             // Digits . Digits ExponentPart FloatTypeSuffix
>      *             // 
>      *             // Since this method allows integer-only strings as input
>      *             // in addition to strings of floating-point literals, the
>      *             // two sub-patterns below are simplifications of the grammar
>      *             // productions from the Java Language Specification, 2nd 
>      *             // edition, section 3.10.2.
>      *
>      *             // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
>      *             "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
>      *
>      *             // . Digits ExponentPart_opt FloatTypeSuffix_opt
>      *             "(\\.("+Digits+")("+Exp+")?))"+
>      *             "[fFdD]?))" +
>      *             "[\\x00-\\x20]*");// Optional trailing &quot;whitespace&quot;
>      *            
>      *        Pattern fpPattern = Pattern.compile(fpRegex);
>      *        if (fpPattern.match(myString))
>      *            Double.valueOf(myString); // Will not throw NumberFormatException
>      *        else {
>      *            // Perform suitable alternative action
>      *        }
>      * </pre>
>      * </code>
>      *


###@###.### 2003-02-13
                                     
2003-02-13
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger

FIXED IN:
tiger

INTEGRATED IN:
tiger
tiger-b05


                                     
2004-06-14



Hardware and Software, Engineered to Work Together