Skip to content

Commit c91fb3f

Browse files
olivergillespiePaul Hohensee
authored and
Paul Hohensee
committed
8313874: JNI NewWeakGlobalRef throws exception for null arg
Reviewed-by: phh Backport-of: 028b3ae1b162bd8f7c340bfa6e9487ca83697955
1 parent 7ea65a0 commit c91fb3f

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/hotspot/share/prims/jni.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2918,7 +2918,7 @@ JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
29182918
HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
29192919
Handle ref_handle(thread, JNIHandles::resolve(ref));
29202920
jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL);
2921-
if (ret == NULL) {
2921+
if (ret == NULL && ref_handle.not_null()) {
29222922
THROW_OOP_(Universe::out_of_memory_error_c_heap(), NULL);
29232923
}
29242924
HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret);

test/hotspot/jtreg/runtime/jni/ReturnJNIWeak/ReturnJNIWeak.java

+10
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,19 @@ private static void testClear() throws Exception {
123123
}
124124
}
125125

126+
// Verify passing a null value returns null and doesn't throw.
127+
private static void testNullValue() {
128+
System.out.println("running testNullValue");
129+
registerObject(null);
130+
if (getObject() != null) {
131+
throw new RuntimeException("expected null");
132+
}
133+
}
134+
126135
public static void main(String[] args) throws Exception {
127136
testSanity();
128137
testSurvival();
129138
testClear();
139+
testNullValue();
130140
}
131141
}

0 commit comments

Comments
 (0)