JDK-5038903 : (cl) ClassLoader.c does not clear a pending exception - JNI spec violation
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:class_loading
  • Affected Version: 1.2.1,5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2004-04-28
  • Updated: 2012-10-08
  • Resolved: 2005-02-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u2Fixed 6 b23Fixed
Related Reports
Relates :  
Description
There is violation to JNI rule in ClassLoader.c, in 1.4.2 and 1.5b48.


====j2se/src/share/native/java/lang/ClassLoader.c in 1.5betab48 ====
........

   329
   330    if ((*env)->ExceptionOccurred(env)) {
   331         JNU_ThrowByNameWithLastError(env, "java/lang/UnsatisfiedLinkError",
   332                                           "exception occurred in JNI_OnLoad");
   333         JVM_UnloadLibrary(handle);
   334         JNU_ReleaseStringPlatformChars(env, name, cname);
   335         return;
   336    }
   337

......

On the URL,
 http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp17626

we can find out the following statement.

.....
Exception Handling

There are two ways to handle an exception in native code:

    * The native method can choose to return immediately, causing the exception
      to be thrown in the Java code that initiated the native method call.
    * The native code can clear the exception by calling ExceptionClear(),
     and then execute its own exception-handling code.

After an exception has been raised, the native code must first clear the exception before making other JNI calls. When there is a pending exception, the only JNI functions that are safe to call are ExceptionOccurred(), ExceptionDescribe(), and ExceptionClear(). The ExceptionDescribe() function prints a debugging message about the pending exception. 
...


In thhe above extracted code, in site an exception occurs,
JNU_ThrowByNameWithLastError(line #331) is called without ExceptionClear.

ExceptionClear() should be done before JNU_ThrowByNameWithLastError.

================================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: dragon
14-06-2004

EVALUATION The submitter is correct (including the suggested fix). This incorrect code has been around since at least 1.2. I have verified that -Xcheck:jni will not trip over this incorrect usage. While the code is technically incorrect, I can not think of any situation in which the addition of the ExceptionClear would change this code's behaviour. We will address problem this in a later release. -- iag@sfbay 2004-04-28
28-04-2004