This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: FYI: PR 2024, plus
- From: Tom Tromey <tromey at redhat dot com>
- To: Java Patch List <java-patches at gcc dot gnu dot org>
- Date: 25 Nov 2001 23:40:53 -0700
- Subject: Patch: FYI: PR 2024, plus
- Reply-to: tromey at redhat dot com
I'm checking this in.
This fixes a couple longstanding bugs in Class.forName. First, the
argument was not verified as a possibly valid class name. Now it is.
Second, you can no longer create an array of `void'. This latter
problem is part of PR 2024, which is now completely fixed.
Tom
Index: ChangeLog
from Tom Tromey <tromey@redhat.com>
Fix for PR libgcj/2024, plus other class name cleanups:
* include/jvm.h (_Jv_VerifyFieldSignature,
_Jv_VerifyMethodSignature, _Jv_VerifyClassName,
_Jv_VerifyIdentifier, _Jv_ClassNameSamePackage): Moved from ...
* include/java-interp.h: ... here.
* defineclass.cc (UTF8_PEEK): No longer conditional on
interpreter.
(_Jv_VerifyOne): Likewise.
(_Jv_VerifyFieldSignature): Likewise.
(_Jv_VerifyMethodSignature): Likewise.
(is_identifier_start): Likewise.
(is_identifier_part): Likewise.
(_Jv_VerifyIdentifier): Likewise.
(_Jv_VerifyClassName): Likewise.
(_Jv_VerifyClassName): Likewise.
(_Jv_ClassNameSamePackage): Likewise.
(_Jv_VerifyClassName): Fail if class name is too long.
* java/lang/natClassLoader.cc (_Jv_NewArrayClass): Disallow array
of void.
* java/lang/natClass.cc (forName): Check syntax of class name.
Include IllegalArgumentException.h.
Index: defineclass.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/defineclass.cc,v
retrieving revision 1.22
diff -u -r1.22 defineclass.cc
--- defineclass.cc 2001/11/05 23:39:54 1.22
+++ defineclass.cc 2001/11/26 06:34:10
@@ -22,8 +22,6 @@
#include <java-interp.h>
-#ifdef INTERPRETER
-
#include <stdlib.h>
#include <java-cpool.h>
#include <gcj/cni.h>
@@ -43,6 +41,8 @@
using namespace gcj;
+#ifdef INTERPRETER
+
// these go in some separate functions, to avoid having _Jv_InitClass
// inserted all over the place.
static void throw_internal_error (char *msg)
@@ -1368,6 +1368,50 @@
::throw_class_format_error (str);
}
+/** Here we define the exceptions that can be thrown */
+
+static void
+throw_no_class_def_found_error (jstring msg)
+{
+ throw (msg
+ ? new java::lang::NoClassDefFoundError (msg)
+ : new java::lang::NoClassDefFoundError);
+}
+
+static void
+throw_no_class_def_found_error (char *msg)
+{
+ throw_no_class_def_found_error (JvNewStringLatin1 (msg));
+}
+
+static void
+throw_class_format_error (jstring msg)
+{
+ throw (msg
+ ? new java::lang::ClassFormatError (msg)
+ : new java::lang::ClassFormatError);
+}
+
+static void
+throw_internal_error (char *msg)
+{
+ throw new java::lang::InternalError (JvNewStringLatin1 (msg));
+}
+
+static void throw_incompatible_class_change_error (jstring msg)
+{
+ throw new java::lang::IncompatibleClassChangeError (msg);
+}
+
+static void throw_class_circularity_error (jstring msg)
+{
+ throw new java::lang::ClassCircularityError (msg);
+}
+
+#endif /* INTERPRETER */
+
+
+
/** This section takes care of verifying integrity of identifiers,
signatures, field ddescriptors, and class names */
@@ -1376,7 +1420,7 @@
int xxch = UTF8_GET(PTR,LIMIT); \
PTR = xxkeep; xxch; })
-/* verify one element of a type descriptor or signature */
+/* Verify one element of a type descriptor or signature. */
static unsigned char*
_Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
{
@@ -1388,7 +1432,8 @@
switch (ch)
{
case 'V':
- if (! void_ok) return 0;
+ if (! void_ok)
+ return 0;
case 'S': case 'B': case 'I': case 'J':
case 'Z': case 'C': case 'F': case 'D':
@@ -1397,16 +1442,18 @@
case 'L':
{
unsigned char *start = ptr, *end;
- do {
- if (ptr > limit)
- return 0;
-
- end = ptr;
-
- if ((ch = UTF8_GET (ptr, limit)) == -1)
- return 0;
-
- } while (ch != ';');
+ do
+ {
+ if (ptr > limit)
+ return 0;
+
+ end = ptr;
+
+ if ((ch = UTF8_GET (ptr, limit)) == -1)
+ return 0;
+
+ }
+ while (ch != ';');
if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
return 0;
}
@@ -1415,18 +1462,15 @@
case '[':
return _Jv_VerifyOne (ptr, limit, false);
break;
-
+
default:
return 0;
}
return ptr;
-
}
-
-/** verification and loading procedures **/
-
+/* Verification and loading procedures. */
bool
_Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
{
@@ -1449,7 +1493,7 @@
while (ptr && UTF8_PEEK (ptr, limit) != ')')
ptr = _Jv_VerifyOne (ptr, limit, false);
-
+
if (UTF8_GET (ptr, limit) != ')')
return false;
@@ -1458,10 +1502,9 @@
return ptr == limit;
}
-
-/* we try to avoid calling the Character methods all the time,
- in fact, they will only be called for non-standard things */
+/* We try to avoid calling the Character methods all the time, in
+ fact, they will only be called for non-standard things. */
static __inline__ int
is_identifier_start (int c)
{
@@ -1522,7 +1565,10 @@
if ('[' == UTF8_PEEK (ptr, limit))
{
- if (! _Jv_VerifyOne (++ptr, limit, false))
+ unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
+ // _Jv_VerifyOne must leave us looking at the terminating nul
+ // byte.
+ if (! end || *end)
return false;
else
return true;
@@ -1554,9 +1600,8 @@
(_Jv_ushort) name->length);
}
-/** returns true, if name1 and name2 represents classes in the same
- package. */
-
+/* Returns true, if NAME1 and NAME2 represent classes in the same
+ package. */
bool
_Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
{
@@ -1571,22 +1616,22 @@
if (ch1 == '.')
last1 = ptr1;
-
+
else if (ch1 == -1)
return false;
}
- // now the length of name1's package name is len
+ // Now the length of NAME1's package name is LEN.
int len = last1 - (unsigned char*) name1->data;
- // if this is longer than name2, then we're off
+ // If this is longer than NAME2, then we're off.
if (len > name2->length)
return false;
- // then compare the first len bytes for equality
+ // Then compare the first len bytes for equality.
if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0)
{
- // check that there are no .'s after position len in name2
+ // Check that there are no .'s after position LEN in NAME2.
unsigned char* ptr2 = (unsigned char*) name2->data + len;
unsigned char* limit2 =
@@ -1602,48 +1647,3 @@
}
return false;
}
-
-
-
-/** Here we define the exceptions that can be thrown */
-
-static void
-throw_no_class_def_found_error (jstring msg)
-{
- throw (msg
- ? new java::lang::NoClassDefFoundError (msg)
- : new java::lang::NoClassDefFoundError);
-}
-
-static void
-throw_no_class_def_found_error (char *msg)
-{
- throw_no_class_def_found_error (JvNewStringLatin1 (msg));
-}
-
-static void
-throw_class_format_error (jstring msg)
-{
- throw (msg
- ? new java::lang::ClassFormatError (msg)
- : new java::lang::ClassFormatError);
-}
-
-static void
-throw_internal_error (char *msg)
-{
- throw new java::lang::InternalError (JvNewStringLatin1 (msg));
-}
-
-static void throw_incompatible_class_change_error (jstring msg)
-{
- throw new java::lang::IncompatibleClassChangeError (msg);
-}
-
-static void throw_class_circularity_error (jstring msg)
-{
- throw new java::lang::ClassCircularityError (msg);
-}
-
-#endif /* INTERPRETER */
-
Index: include/java-interp.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/java-interp.h,v
retrieving revision 1.15
diff -u -r1.15 java-interp.h
--- include/java-interp.h 2001/11/05 23:39:54 1.15
+++ include/java-interp.h 2001/11/26 06:34:11
@@ -33,12 +33,6 @@
struct _Jv_ResolvedMethod;
-bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
-bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
-bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
-bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
-bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
-bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
void _Jv_DefineClass (jclass, jbyteArray, jint, jint);
void _Jv_InitField (jobject, jclass, int);
Index: include/jvm.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/include/jvm.h,v
retrieving revision 1.45
diff -u -r1.45 jvm.h
--- include/jvm.h 2001/10/23 05:42:03 1.45
+++ include/jvm.h 2001/11/26 06:34:11
@@ -341,6 +341,14 @@
struct _Jv_JavaVM;
_Jv_JavaVM *_Jv_GetJavaVM ();
+// Some verification functions from defineclass.cc.
+bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
+bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
+bool _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length);
+bool _Jv_VerifyClassName (_Jv_Utf8Const *name);
+bool _Jv_VerifyIdentifier (_Jv_Utf8Const *);
+bool _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2);
+
#ifdef ENABLE_JVMPI
#include "jvmpi.h"
Index: java/lang/natClass.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClass.cc,v
retrieving revision 1.46
diff -u -r1.46 natClass.cc
--- java/lang/natClass.cc 2001/10/16 08:35:17 1.46
+++ java/lang/natClass.cc 2001/11/26 06:34:13
@@ -34,6 +34,7 @@
#include <java/lang/ExceptionInInitializerError.h>
#include <java/lang/IllegalAccessException.h>
#include <java/lang/IllegalAccessError.h>
+#include <java/lang/IllegalArgumentException.h>
#include <java/lang/IncompatibleClassChangeError.h>
#include <java/lang/InstantiationException.h>
#include <java/lang/NoClassDefFoundError.h>
@@ -75,9 +76,10 @@
char buffer[length];
_Jv_GetStringUTFRegion (className, 0, length, buffer);
- // FIXME: should check syntax of CLASSNAME and throw
- // IllegalArgumentException on failure.
_Jv_Utf8Const *name = _Jv_makeUtf8Const (buffer, length);
+
+ if (! _Jv_VerifyClassName (name))
+ throw new java::lang::ClassNotFoundException (className);
// FIXME: should use bootstrap class loader if loader is null.
jclass klass = (buffer[0] == '['
Index: java/lang/natClassLoader.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natClassLoader.cc,v
retrieving revision 1.42
diff -u -r1.42 natClassLoader.cc
--- java/lang/natClassLoader.cc 2001/10/31 00:48:16 1.42
+++ java/lang/natClassLoader.cc 2001/11/26 06:34:13
@@ -598,7 +598,11 @@
return;
if (element->isPrimitive())
- len = 3;
+ {
+ if (element == JvPrimClass (void))
+ throw new java::lang::ClassNotFoundException ();
+ len = 3;
+ }
else
len = element->name->length + 5;