Skip to content

Commit b3243b7

Browse files
committed
Fix CRL callback thread leak
1 parent f3e8cc0 commit b3243b7

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

native/com_wolfssl_WolfSSLContext.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,7 @@ void NativeCtxMissingCRLCallback(const char* url)
17771777
{
17781778
JNIEnv* jenv;
17791779
jint vmret = 0;
1780+
int needsDetach = 0;
17801781
jclass excClass;
17811782
jclass crlClass = NULL;
17821783
jmethodID crlMethod;
@@ -1795,6 +1796,7 @@ void NativeCtxMissingCRLCallback(const char* url)
17951796
printf("Failed to attach JNIEnv to thread\n");
17961797
return;
17971798
}
1799+
needsDetach = 1;
17981800
} else if (vmret != JNI_OK) {
17991801
printf("Unable to get JNIEnv from JavaVM\n");
18001802
return;
@@ -1805,6 +1807,8 @@ void NativeCtxMissingCRLCallback(const char* url)
18051807
if ((*jenv)->ExceptionOccurred(jenv)) {
18061808
(*jenv)->ExceptionDescribe(jenv);
18071809
(*jenv)->ExceptionClear(jenv);
1810+
if (needsDetach)
1811+
(*g_vm)->DetachCurrentThread(g_vm);
18081812
return;
18091813
}
18101814

@@ -1817,6 +1821,8 @@ void NativeCtxMissingCRLCallback(const char* url)
18171821
if (!crlClass) {
18181822
(*jenv)->ThrowNew(jenv, excClass,
18191823
"Can't get native WolfSSLMissingCRLCallback class reference");
1824+
if (needsDetach)
1825+
(*g_vm)->DetachCurrentThread(g_vm);
18201826
return;
18211827
}
18221828

@@ -1831,6 +1837,8 @@ void NativeCtxMissingCRLCallback(const char* url)
18311837

18321838
(*jenv)->ThrowNew(jenv, excClass,
18331839
"Error getting missingCRLCallback method from JNI");
1840+
if (needsDetach)
1841+
(*g_vm)->DetachCurrentThread(g_vm);
18341842
return;
18351843
}
18361844

@@ -1843,7 +1851,6 @@ void NativeCtxMissingCRLCallback(const char* url)
18431851
if ((*jenv)->ExceptionOccurred(jenv)) {
18441852
(*jenv)->ExceptionDescribe(jenv);
18451853
(*jenv)->ExceptionClear(jenv);
1846-
return;
18471854
}
18481855

18491856
} else {
@@ -1855,6 +1862,9 @@ void NativeCtxMissingCRLCallback(const char* url)
18551862
(*jenv)->ThrowNew(jenv, excClass,
18561863
"Object reference invalid in NativeMissingCRLCallback");
18571864
}
1865+
1866+
if (needsDetach)
1867+
(*g_vm)->DetachCurrentThread(g_vm);
18581868
}
18591869

18601870
#endif /* HAVE_CRL */

native/com_wolfssl_WolfSSLSession.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3869,6 +3869,7 @@ void NativeMissingCRLCallback(const char* url)
38693869
{
38703870
JNIEnv* jenv;
38713871
jint vmret = 0;
3872+
int needsDetach = 0;
38723873
jclass crlClass = NULL;
38733874
jmethodID crlMethod = NULL;
38743875
jobjectRefType refcheck;
@@ -3886,6 +3887,7 @@ void NativeMissingCRLCallback(const char* url)
38863887
printf("Failed to attach JNIEnv to thread\n");
38873888
return;
38883889
}
3890+
needsDetach = 1;
38893891
} else if (vmret != JNI_OK) {
38903892
printf("Unable to get JNIEnv from JavaVM\n");
38913893
return;
@@ -3900,6 +3902,8 @@ void NativeMissingCRLCallback(const char* url)
39003902
if (!crlClass) {
39013903
throwWolfSSLException(jenv,
39023904
"Can't get native WolfSSLMissingCRLCallback class reference");
3905+
if (needsDetach)
3906+
(*g_vm)->DetachCurrentThread(g_vm);
39033907
return;
39043908
}
39053909

@@ -3914,6 +3918,8 @@ void NativeMissingCRLCallback(const char* url)
39143918

39153919
throwWolfSSLException(jenv,
39163920
"Error getting missingCRLCallback method from JNI");
3921+
if (needsDetach)
3922+
(*g_vm)->DetachCurrentThread(g_vm);
39173923
return;
39183924
}
39193925

@@ -3925,7 +3931,6 @@ void NativeMissingCRLCallback(const char* url)
39253931
if ((*jenv)->ExceptionOccurred(jenv)) {
39263932
(*jenv)->ExceptionDescribe(jenv);
39273933
(*jenv)->ExceptionClear(jenv);
3928-
return;
39293934
}
39303935

39313936
} else {
@@ -3937,6 +3942,9 @@ void NativeMissingCRLCallback(const char* url)
39373942
throwWolfSSLException(jenv,
39383943
"Object reference invalid in NativeMissingCRLCallback");
39393944
}
3945+
3946+
if (needsDetach)
3947+
(*g_vm)->DetachCurrentThread(g_vm);
39403948
}
39413949

39423950
#endif /* HAVE_CRL */

0 commit comments

Comments
 (0)