diff options
author | Per Bothner <bothner@gcc.gnu.org> | 2001-03-23 16:59:57 -0800 |
---|---|---|
committer | Per Bothner <bothner@gcc.gnu.org> | 2001-03-23 16:59:57 -0800 |
commit | 3c45ffa5146a7132ea4867981f0d2a737d656e54 (patch) | |
tree | fafc2f660ada28148b694ca4afb1638030488771 /libjava | |
parent | 7a3155bef708a70dfa72958ef23fc2356b20c8a2 (diff) | |
download | gcc-3c45ffa5146a7132ea4867981f0d2a737d656e54.zip gcc-3c45ffa5146a7132ea4867981f0d2a737d656e54.tar.gz gcc-3c45ffa5146a7132ea4867981f0d2a737d656e54.tar.bz2 |
natDouble.cc (parseDouble): Cannot use errno to check for errors...
* java/lang/natDouble.cc (parseDouble): Cannot use errno to
check for errors, since we don't want to throw exception on
overflow/underflow. Instead, trim whitespace, and then check that
_strtod_r uses up all the rest of the string.
From-SVN: r40800
Diffstat (limited to 'libjava')
-rw-r--r-- | libjava/java/lang/natDouble.cc | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/libjava/java/lang/natDouble.cc b/libjava/java/lang/natDouble.cc index 9ed7e539..af2d70a 100644 --- a/libjava/java/lang/natDouble.cc +++ b/libjava/java/lang/natDouble.cc @@ -15,6 +15,7 @@ details. */ #include <gcj/cni.h> #include <java/lang/String.h> #include <java/lang/Double.h> +#include <java/lang/Character.h> #include <java/lang/NumberFormatException.h> #include <jvm.h> @@ -160,19 +161,28 @@ jdouble java::lang::Double::parseDouble(jstring str) { int length = str->length(); - // Note that UTF can expand 3x. - - char *data = (char *) __builtin_alloca (3 * length + 1); - - data[_Jv_GetStringUTFRegion (str, 0, length, data)] = 0; - - struct _Jv_reent reent; - memset (&reent, 0, sizeof reent); - - double val = _strtod_r (&reent, data, NULL); - - if (reent._errno) - _Jv_Throw (new NumberFormatException); - - return val; + while (length > 0 + && Character::isWhitespace(str->charAt(length - 1))) + length--; + jsize start = 0; + while (length > 0 + && Character::isWhitespace(str->charAt(start))) + start++, length--; + + if (length > 0) + { + // Note that UTF can expand 3x. + char *data = (char *) __builtin_alloca (3 * length + 1); + jsize blength = _Jv_GetStringUTFRegion (str, start, length, data); + data[blength] = 0; + + struct _Jv_reent reent; + memset (&reent, 0, sizeof reent); + + char *endptr; + double val = _strtod_r (&reent, data, &endptr); + if (endptr == data + blength) + return val; + } + _Jv_Throw (new NumberFormatException); } |