bug in Integer.decode ?

Archie Cobbs archie at whistle.com
Thu Aug 13 11:29:19 PDT 1998


Enno Rehling writes:
> I couldn't resist. There's a bug in kaffe's Integer.java, just as I
> expected: when the Integer is not prefixed by 0x, O or #, it's not correct
> to do nm.substring(1), just nm is okay.

There are a few other problems with Integer.java..

 - decode() and getInteger() will not decode negative integers,
   throwing NumberFormatException instead.

 - decode() will successfully parse a string like "0x-1234", which
   instead should generate a NumberFormatException.

 - getInteger() duplicates the code in decode()

The patch below should fix these problems.

-Archie

___________________________________________________________________________
Archie Cobbs   *   Whistle Communications, Inc.  *   http://www.whistle.com

Index: external-source/ChangeLog
===================================================================
RCS file: /cvs/mod/net/kaffe/ChangeLog,v
retrieving revision 1.1.1.6.2.14
diff -c -u -r1.1.1.6.2.14 ChangeLog
--- ChangeLog	1998/08/12 16:32:57	1.1.1.6.2.14
+++ ChangeLog	1998/08/13 18:26:46
@@ -1,3 +1,8 @@
+Thu Aug 13 10:55:45 1998  Archie Cobbs  <archie at whistle.com>
+
+	* libraries/javalib/java/lang/Integer.java: fix decoding
+	  bugs in decode() and getInteger() methods.
+
 Mon Aug 10 19:58:36 1998  Alexandre Oliva  <oliva at dcc.unicamp.br>
 
 	* kaffe/kaffevm/jni.c (Kaffe_JNI_wrapper): call installMethodCode
Index: external-source/libraries/javalib/java/lang/Integer.java
===================================================================
RCS file: /cvs/mod/net/kaffe/libraries/javalib/java/lang/Integer.java,v
retrieving revision 1.1.1.1.2.3
diff -c -u -r1.1.1.1.2.3 Integer.java
--- Integer.java	1998/08/06 00:43:42	1.1.1.1.2.3
+++ Integer.java	1998/08/13 18:26:52
@@ -33,18 +33,31 @@
 
 public static Integer decode(String nm) throws NumberFormatException
 {
+	/* Strip off negative sign, if any */
+	int sign = 1;
+	if (nm.startsWith("-")) {
+		sign = -1;
+		nm = nm.substring(1);
+	}
+	/* Strip off base indicator, if any */
+	int base = 10;
 	if (nm.startsWith("0x")) {
-		return (Integer.valueOf(nm.substring(2), 16));
+		base = 16;
+		nm = nm.substring(2);
 	}
 	else if (nm.startsWith("#")) {
-		return (Integer.valueOf(nm.substring(1), 16));
+		base = 16;
+		nm = nm.substring(1);
 	}
 	else if (nm.startsWith("0")) {
-		return (Integer.valueOf(nm.substring(1), 8));
+		base = 8;
+		nm = nm.substring(1);
 	}
-	else {
-		return (Integer.valueOf(nm.substring(1), 10));
+	/* A string like "0x-1234" must generate an error; disallow it here */
+	if (nm.startsWith("-")) {
+		throw new NumberFormatException();
 	}
+	return new Integer(sign * parseInt(nm, base));
 }
 
 public double doubleValue() {
@@ -74,13 +87,10 @@
 	if (val==null) arg=null; else arg=val.toString();
 
 	String prop=System.getProperty(nm, arg);
+	if (prop==null) return val;
 
 	try {
-		if (prop==null) return val;
-		else if (prop.startsWith("0x")) return Integer.valueOf(prop.substring(2), 16);
-		else if (prop.startsWith("#")) return Integer.valueOf(prop.substring(1), 16);
-		else if (prop.startsWith("0")) return Integer.valueOf(prop.substring(1), 8);
-		else return new Integer(prop);
+		return decode(prop);
 	}
 	catch (NumberFormatException e) {
 		return val;


More information about the kaffe mailing list