natDouble.cc::parseDouble patch

Per Bothner per@bothner.com
Fri Mar 23 17:00:00 GMT 2001


I'm checking this into both the trunk and the gcc3 branch.
Without it, the Kawa testsuite gets an erroneous exception.

2001-03-23  Per Bothner  <per@bothner.com>

	* 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.

Index: java/lang/natDouble.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natDouble.cc,v
retrieving revision 1.11.2.1
diff -u -p -r1.11.2.1 natDouble.cc
--- natDouble.cc	2001/02/17 01:06:45	1.11.2.1
+++ natDouble.cc	2001/03/24 00:30:56
@@ -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.
+  while (length > 0
+	 && Character::isWhitespace(str->charAt(length - 1)))
+    length--;
+  jsize start = 0;
+  while (length > 0
+	 && Character::isWhitespace(str->charAt(start)))
+    start++, length--;
 
-  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;
+  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);
 }

-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/



More information about the Java-patches mailing list