8031588: warnings from b03 for jdk/src/share/native/sun/security/jgss/wrapper: JNI exception pending

Reviewed-by: chegar, mullan
This commit is contained in:
Michael McMahon 2014-02-06 22:12:09 +00:00
parent 79938735a2
commit 3981cc3f41
3 changed files with 83 additions and 18 deletions

View File

@ -28,6 +28,7 @@
#include "NativeFunc.h" #include "NativeFunc.h"
#include "jlong.h" #include "jlong.h"
#include <jni.h> #include <jni.h>
#include "jni_util.h"
/* Throws a Java Exception by name */ /* Throws a Java Exception by name */
@ -36,6 +37,7 @@ void throwByName(JNIEnv *env, const char *name, const char *msg) {
if (cls != 0) /* Otherwise an exception has already been thrown */ if (cls != 0) /* Otherwise an exception has already been thrown */
(*env)->ThrowNew(env, cls, msg); (*env)->ThrowNew(env, cls, msg);
JNU_Equals(env, NULL, NULL);
} }
void throwOutOfMemoryError(JNIEnv *env, const char *message) { void throwOutOfMemoryError(JNIEnv *env, const char *message) {
@ -65,6 +67,12 @@ Java_sun_security_jgss_wrapper_GSSLibStub_init(JNIEnv *env,
} }
libName = (*env)->GetStringUTFChars(env, jlibName, NULL); libName = (*env)->GetStringUTFChars(env, jlibName, NULL);
if (libName == NULL) {
if (!(*env)->ExceptionCheck(env)) {
throwOutOfMemoryError(env, NULL);
}
return JNI_FALSE;
}
sprintf(debugBuf, "[GSSLibStub_init] libName=%s", libName); sprintf(debugBuf, "[GSSLibStub_init] libName=%s", libName);
debug(env, debugBuf); debug(env, debugBuf);
@ -110,6 +118,8 @@ Java_sun_security_jgss_wrapper_GSSLibStub_getMechPtr(JNIEnv *env,
} }
} }
(*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0); (*env)->ReleaseByteArrayElements(env, jbytes, bytes, 0);
} else {
JNU_CHECK_EXCEPTION_RETURN(env, jlong_zero);
} }
if (found != JNI_TRUE) { if (found != JNI_TRUE) {
checkStatus(env, NULL, GSS_S_BAD_MECH, 0, "[GSSLibStub_getMechPtr]"); checkStatus(env, NULL, GSS_S_BAD_MECH, 0, "[GSSLibStub_getMechPtr]");
@ -147,7 +157,9 @@ gss_channel_bindings_t getGSSCB(JNIEnv *env, jobject jcb) {
cb->initiator_addrtype = GSS_C_AF_INET; cb->initiator_addrtype = GSS_C_AF_INET;
value = (*env)->CallObjectMethod(env, jinetAddr, value = (*env)->CallObjectMethod(env, jinetAddr,
MID_InetAddress_getAddr); MID_InetAddress_getAddr);
initGSSBuffer(env, value, &(cb->initiator_address)); if (!initGSSBuffer(env, value, &(cb->initiator_address))) {
return NULL;
}
} else { } else {
cb->initiator_addrtype = GSS_C_AF_NULLADDR; cb->initiator_addrtype = GSS_C_AF_NULLADDR;
cb->initiator_address.length = 0; cb->initiator_address.length = 0;
@ -161,7 +173,9 @@ gss_channel_bindings_t getGSSCB(JNIEnv *env, jobject jcb) {
cb->acceptor_addrtype = GSS_C_AF_INET; cb->acceptor_addrtype = GSS_C_AF_INET;
value = (*env)->CallObjectMethod(env, jinetAddr, value = (*env)->CallObjectMethod(env, jinetAddr,
MID_InetAddress_getAddr); MID_InetAddress_getAddr);
initGSSBuffer(env, value, &(cb->acceptor_address)); if (!initGSSBuffer(env, value, &(cb->acceptor_address))) {
return NULL;
}
} else { } else {
cb->acceptor_addrtype = GSS_C_AF_NULLADDR; cb->acceptor_addrtype = GSS_C_AF_NULLADDR;
cb->acceptor_address.length = 0; cb->acceptor_address.length = 0;
@ -171,7 +185,9 @@ gss_channel_bindings_t getGSSCB(JNIEnv *env, jobject jcb) {
value = (*env)->CallObjectMethod(env, jcb, value = (*env)->CallObjectMethod(env, jcb,
MID_ChannelBinding_getAppData); MID_ChannelBinding_getAppData);
if (value != NULL) { if (value != NULL) {
initGSSBuffer(env, value, &(cb->application_data)); if (!initGSSBuffer(env, value, &(cb->application_data))) {
return NULL;
}
} else { } else {
cb->application_data.length = 0; cb->application_data.length = 0;
cb->application_data.value = NULL; cb->application_data.value = NULL;
@ -230,6 +246,7 @@ void setSupplementaryInfo(JNIEnv *env, jobject jstub, jobject jprop,
isUnseq = ((suppInfo & GSS_S_UNSEQ_TOKEN) != 0); isUnseq = ((suppInfo & GSS_S_UNSEQ_TOKEN) != 0);
hasGap = ((suppInfo & GSS_S_GAP_TOKEN) != 0); hasGap = ((suppInfo & GSS_S_GAP_TOKEN) != 0);
minorMsg = getMinorMessage(env, jstub, minor); minorMsg = getMinorMessage(env, jstub, minor);
CHECK_NULL(minorMsg);
(*env)->CallVoidMethod(env, jprop, MID_MessageProp_setSupplementaryStates, (*env)->CallVoidMethod(env, jprop, MID_MessageProp_setSupplementaryStates,
isDuplicate, isOld, isUnseq, hasGap, minor, isDuplicate, isOld, isUnseq, hasGap, minor,
minorMsg); minorMsg);
@ -277,6 +294,7 @@ Java_sun_security_jgss_wrapper_GSSLibStub_inquireNamesForMech(JNIEnv *env,
/* release intermediate buffers */ /* release intermediate buffers */
deleteGSSOIDSet(nameTypes); deleteGSSOIDSet(nameTypes);
CHECK_NULL_RETURN(result, NULL);
checkStatus(env, jobj, major, minor, "[GSSLibStub_inquireNamesForMech]"); checkStatus(env, jobj, major, minor, "[GSSLibStub_inquireNamesForMech]");
return result; return result;
} else return NULL; } else return NULL;
@ -326,7 +344,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_importName(JNIEnv *env,
debug(env, "[GSSLibStub_importName]"); debug(env, "[GSSLibStub_importName]");
initGSSBuffer(env, jnameVal, &nameVal); if (!initGSSBuffer(env, jnameVal, &nameVal)) {
return jlong_zero;
}
nameType = newGSSOID(env, jnameType); nameType = newGSSOID(env, jnameType);
if ((*env)->ExceptionCheck(env)) { if ((*env)->ExceptionCheck(env)) {
deleteGSSOID(nameType); deleteGSSOID(nameType);
@ -501,9 +521,14 @@ Java_sun_security_jgss_wrapper_GSSLibStub_displayName(JNIEnv *env,
/* release intermediate buffers */ /* release intermediate buffers */
jname = getJavaString(env, &outNameBuf); jname = getJavaString(env, &outNameBuf);
if (jname == NULL && !(*env)->ExceptionCheck(env)) {
throwOutOfMemoryError(env, NULL);
return NULL;
}
jtype = getJavaOID(env, outNameType); jtype = getJavaOID(env, outNameType);
jresult = (*env)->NewObjectArray(env, 2, CLS_Object, NULL); jresult = (*env)->NewObjectArray(env, 2, CLS_Object, NULL);
CHECK_NULL_RETURN(jresult, NULL);
/* return immediately if an exception has occurred */ /* return immediately if an exception has occurred */
if ((*env)->ExceptionCheck(env)) { if ((*env)->ExceptionCheck(env)) {
@ -738,7 +763,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_importContext(JNIEnv *env,
debug(env, "[GSSLibStub_importContext]"); debug(env, "[GSSLibStub_importContext]");
contextHdl = GSS_C_NO_CONTEXT; contextHdl = GSS_C_NO_CONTEXT;
initGSSBuffer(env, jctxtToken, &ctxtToken); if (!initGSSBuffer(env, jctxtToken, &ctxtToken)) {
return NULL;
}
/* gss_import_sec_context(...) => GSS_S_NO_CONTEXT, GSS_S_DEFECTIVE_TOKEN, /* gss_import_sec_context(...) => GSS_S_NO_CONTEXT, GSS_S_DEFECTIVE_TOKEN,
GSS_S_UNAVAILABLE, GSS_S_UNAUTHORIZED */ GSS_S_UNAVAILABLE, GSS_S_UNAUTHORIZED */
@ -829,7 +856,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_initContext(JNIEnv *env,
return NULL; return NULL;
} }
initGSSBuffer(env, jinToken, &inToken); if (!initGSSBuffer(env, jinToken, &inToken)) {
return NULL;
}
sprintf(debugBuf, sprintf(debugBuf,
"[GSSLibStub_initContext] before: pCred=%ld, pContext=%ld", "[GSSLibStub_initContext] before: pCred=%ld, pContext=%ld",
@ -927,7 +956,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_acceptContext(JNIEnv *env,
contextHdl = (gss_ctx_id_t)jlong_to_ptr( contextHdl = (gss_ctx_id_t)jlong_to_ptr(
(*env)->GetLongField(env, jcontextSpi, FID_NativeGSSContext_pContext)); (*env)->GetLongField(env, jcontextSpi, FID_NativeGSSContext_pContext));
credHdl = (gss_cred_id_t) jlong_to_ptr(pCred); credHdl = (gss_cred_id_t) jlong_to_ptr(pCred);
initGSSBuffer(env, jinToken, &inToken); if (!initGSSBuffer(env, jinToken, &inToken)) {
return NULL;
}
cb = getGSSCB(env, jcb); cb = getGSSCB(env, jcb);
if ((*env)->ExceptionCheck(env)) { if ((*env)->ExceptionCheck(env)) {
free(cb); free(cb);
@ -1102,6 +1133,7 @@ Java_sun_security_jgss_wrapper_GSSLibStub_inquireContext(JNIEnv *env,
result[5] = (jlong) getJavaTime(time); result[5] = (jlong) getJavaTime(time);
jresult = (*env)->NewLongArray(env, 6); jresult = (*env)->NewLongArray(env, 6);
CHECK_NULL_RETURN(jresult, NULL);
(*env)->SetLongArrayRegion(env, jresult, 0, 6, result); (*env)->SetLongArrayRegion(env, jresult, 0, 6, result);
/* release intermediate buffers */ /* release intermediate buffers */
@ -1335,7 +1367,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_getMic(JNIEnv *env, jobject jobj,
} }
contextHdl = (gss_ctx_id_t) jlong_to_ptr(pContext); contextHdl = (gss_ctx_id_t) jlong_to_ptr(pContext);
qop = (gss_qop_t) jqop; qop = (gss_qop_t) jqop;
initGSSBuffer(env, jmsg, &msg); if (!initGSSBuffer(env, jmsg, &msg)) {
return NULL;
}
/* gss_get_mic(...) => GSS_S_CONTEXT_EXPIRED, GSS_S_NO_CONTEXT(!), /* gss_get_mic(...) => GSS_S_CONTEXT_EXPIRED, GSS_S_NO_CONTEXT(!),
GSS_S_BAD_QOP */ GSS_S_BAD_QOP */
@ -1379,8 +1413,12 @@ Java_sun_security_jgss_wrapper_GSSLibStub_verifyMic(JNIEnv *env,
"[GSSLibStub_verifyMic]"); "[GSSLibStub_verifyMic]");
return; return;
} }
initGSSBuffer(env, jmsg, &msg);
initGSSBuffer(env, jmsgToken, &msgToken); if (!initGSSBuffer(env, jmsg, &msg) ||
!initGSSBuffer(env, jmsgToken, &msgToken)) {
return;
}
qop = (gss_qop_t) (*env)->CallIntMethod(env, jprop, MID_MessageProp_getQOP); qop = (gss_qop_t) (*env)->CallIntMethod(env, jprop, MID_MessageProp_getQOP);
/* gss_verify_mic(...) => GSS_S_DEFECTIVE_TOKEN, GSS_S_BAD_MIC, /* gss_verify_mic(...) => GSS_S_DEFECTIVE_TOKEN, GSS_S_BAD_MIC,
GSS_S_CONTEXT_EXPIRED, GSS_S_DUPLICATE_TOKEN(!), GSS_S_OLD_TOKEN(!), GSS_S_CONTEXT_EXPIRED, GSS_S_DUPLICATE_TOKEN(!), GSS_S_OLD_TOKEN(!),
@ -1433,7 +1471,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_wrap(JNIEnv *env,
(*env)->CallBooleanMethod(env, jprop, MID_MessageProp_getPrivacy); (*env)->CallBooleanMethod(env, jprop, MID_MessageProp_getPrivacy);
qop = (gss_qop_t) qop = (gss_qop_t)
(*env)->CallIntMethod(env, jprop, MID_MessageProp_getQOP); (*env)->CallIntMethod(env, jprop, MID_MessageProp_getQOP);
initGSSBuffer(env, jmsg, &msg); if (!initGSSBuffer(env, jmsg, &msg)) {
return NULL;
}
/* gss_wrap(...) => GSS_S_CONTEXT_EXPIRED, GSS_S_NO_CONTEXT(!), /* gss_wrap(...) => GSS_S_CONTEXT_EXPIRED, GSS_S_NO_CONTEXT(!),
GSS_S_BAD_QOP */ GSS_S_BAD_QOP */
major = (*ftab->wrap)(&minor, contextHdl, confFlag, qop, &msg, &confState, major = (*ftab->wrap)(&minor, contextHdl, confFlag, qop, &msg, &confState,
@ -1479,7 +1519,9 @@ Java_sun_security_jgss_wrapper_GSSLibStub_unwrap(JNIEnv *env,
checkStatus(env, jobj, GSS_S_CONTEXT_EXPIRED, 0, "[GSSLibStub_unwrap]"); checkStatus(env, jobj, GSS_S_CONTEXT_EXPIRED, 0, "[GSSLibStub_unwrap]");
return NULL; return NULL;
} }
initGSSBuffer(env, jmsgToken, &msgToken); if (!initGSSBuffer(env, jmsgToken, &msgToken)) {
return NULL;
}
confState = 0; confState = 0;
qop = GSS_C_QOP_DEFAULT; qop = GSS_C_QOP_DEFAULT;
/* gss_unwrap(...) => GSS_S_DEFECTIVE_TOKEN, GSS_S_BAD_MIC, /* gss_unwrap(...) => GSS_S_DEFECTIVE_TOKEN, GSS_S_BAD_MIC,

View File

@ -27,6 +27,7 @@
#include "NativeFunc.h" #include "NativeFunc.h"
#include "jlong.h" #include "jlong.h"
#include <jni.h> #include <jni.h>
#include "jni_util.h"
extern void throwOutOfMemoryError(JNIEnv *env, const char *message); extern void throwOutOfMemoryError(JNIEnv *env, const char *message);
@ -495,6 +496,9 @@ jstring getMinorMessage(JNIEnv *env, jobject jstub, OM_uint32 statusValue) {
&messageContext, &statusString); &messageContext, &statusString);
/* release intermediate buffers */ /* release intermediate buffers */
msg = getJavaString(env, &statusString); msg = getJavaString(env, &statusString);
if (msg == NULL && !(*env)->ExceptionCheck(env)) {
throwOutOfMemoryError(env, NULL);
}
(*ftab->releaseBuffer)(&minor, &statusString); (*ftab->releaseBuffer)(&minor, &statusString);
return msg; return msg;
} }
@ -529,6 +533,7 @@ void checkStatus(JNIEnv *env, jobject jstub, OM_uint32 major,
jmsg = NULL; jmsg = NULL;
if (minor != 0) { if (minor != 0) {
jmsg = getMinorMessage(env, jstub, minor); jmsg = getMinorMessage(env, jstub, minor);
CHECK_NULL(jmsg);
} }
gssEx = (*env)->NewObject(env, CLS_GSSException, gssEx = (*env)->NewObject(env, CLS_GSSException,
MID_GSSException_ctor3, MID_GSSException_ctor3,
@ -545,28 +550,41 @@ void checkStatus(JNIEnv *env, jobject jstub, OM_uint32 major,
} }
jmajor = 13; /* use GSSException.FAILURE for now */ jmajor = 13; /* use GSSException.FAILURE for now */
jmsg = (*env)->NewStringUTF(env, msg); jmsg = (*env)->NewStringUTF(env, msg);
CHECK_NULL(jmsg);
gssEx = (*env)->NewObject(env, CLS_GSSException, gssEx = (*env)->NewObject(env, CLS_GSSException,
MID_GSSException_ctor3, MID_GSSException_ctor3,
jmajor, jminor, jmsg); jmajor, jminor, jmsg);
CHECK_NULL(gssEx);
(*env)->Throw(env, gssEx); (*env)->Throw(env, gssEx);
} }
} }
/* /*
* Utility routine for initializing gss_buffer_t structure * Utility routine for initializing gss_buffer_t structure
* with the byte[] in the specified jbyteArray object. * with the byte[] in the specified jbyteArray object.
* NOTE: need to call resetGSSBuffer(...) to free up * NOTE: need to call resetGSSBuffer(...) to free up
* the resources. * the resources.
* Return JNI_TRUE if GetByteArrayElements() returns ok, JNI_FALSE otherwise
* If JNI_FALSE returned, then an exception has been thrown.
*/ */
void initGSSBuffer(JNIEnv *env, jbyteArray jbytes, int initGSSBuffer(JNIEnv *env, jbyteArray jbytes,
gss_buffer_t cbytes) { gss_buffer_t cbytes) {
if (jbytes != NULL) { if (jbytes != NULL) {
cbytes->length = (*env)->GetArrayLength(env, jbytes); cbytes->length = (*env)->GetArrayLength(env, jbytes);
cbytes->value = (*env)->GetByteArrayElements(env, jbytes, NULL); cbytes->value = (*env)->GetByteArrayElements(env, jbytes, NULL);
if (cbytes->value == NULL) {
if (!(*env)->ExceptionCheck(env)) {
throwOutOfMemoryError(env, NULL);
}
return JNI_FALSE;
}
} else { } else {
cbytes->length = 0; cbytes->length = 0;
cbytes->value = NULL; cbytes->value = NULL;
} }
return JNI_TRUE;
} }
/* /*
* Utility routine for unpinning/releasing the byte[] * Utility routine for unpinning/releasing the byte[]
* associated with the specified jbyteArray object. * associated with the specified jbyteArray object.
@ -593,7 +611,8 @@ jbyteArray getJavaBuffer(JNIEnv *env, gss_buffer_t cbytes) {
if ((cbytes != NULL) && (cbytes != GSS_C_NO_BUFFER) && if ((cbytes != NULL) && (cbytes != GSS_C_NO_BUFFER) &&
(cbytes->length != 0)) { (cbytes->length != 0)) {
result = (*env)->NewByteArray(env, cbytes->length); result = (*env)->NewByteArray(env, cbytes->length);
(*env)->SetByteArrayRegion(env, result, 0, cbytes->length, if (result != NULL)
(*env)->SetByteArrayRegion(env, result, 0, cbytes->length,
cbytes->value); cbytes->value);
(*ftab->releaseBuffer)(&minor, cbytes); (*ftab->releaseBuffer)(&minor, cbytes);
return result; return result;
@ -616,6 +635,7 @@ gss_OID newGSSOID(JNIEnv *env, jobject jOid) {
if ((*env)->ExceptionCheck(env)) { if ((*env)->ExceptionCheck(env)) {
gssEx = (*env)->ExceptionOccurred(env); gssEx = (*env)->ExceptionOccurred(env);
(*env)->Throw(env, gssEx); (*env)->Throw(env, gssEx);
return GSS_C_NO_OID;
} }
cOid = malloc(sizeof(struct gss_OID_desc_struct)); cOid = malloc(sizeof(struct gss_OID_desc_struct));
if (cOid == NULL) { if (cOid == NULL) {
@ -665,13 +685,12 @@ jobject getJavaOID(JNIEnv *env, gss_OID cOid) {
oidHdr[0] = 6; oidHdr[0] = 6;
oidHdr[1] = cLen; oidHdr[1] = cLen;
jbytes = (*env)->NewByteArray(env, cLen+2); jbytes = (*env)->NewByteArray(env, cLen+2);
CHECK_NULL_RETURN(jbytes, NULL);
(*env)->SetByteArrayRegion(env, jbytes, 0, 2, (jbyte *) oidHdr); (*env)->SetByteArrayRegion(env, jbytes, 0, 2, (jbyte *) oidHdr);
(*env)->SetByteArrayRegion(env, jbytes, 2, cLen, (jbyte *) cOid->elements); (*env)->SetByteArrayRegion(env, jbytes, 2, cLen, (jbyte *) cOid->elements);
result = (*env)->NewObject(env, CLS_Oid, MID_Oid_ctor1, jbytes); result = (*env)->NewObject(env, CLS_Oid, MID_Oid_ctor1, jbytes);
if ((*env)->ExceptionCheck(env)) { JNU_CHECK_EXCEPTION_RETURN(env, NULL);
(*env)->Throw(env, (*env)->ExceptionOccurred(env));
}
(*env)->DeleteLocalRef(env, jbytes); (*env)->DeleteLocalRef(env, jbytes);
return result; return result;
} }
@ -722,6 +741,9 @@ jobjectArray getJavaOIDArray(JNIEnv *env, gss_OID_set cOidSet) {
if (cOidSet != NULL && cOidSet != GSS_C_NO_OID_SET) { if (cOidSet != NULL && cOidSet != GSS_C_NO_OID_SET) {
numOfOids = cOidSet->count; numOfOids = cOidSet->count;
jOidSet = (*env)->NewObjectArray(env, numOfOids, CLS_Oid, NULL); jOidSet = (*env)->NewObjectArray(env, numOfOids, CLS_Oid, NULL);
if (jOidSet == NULL) {
return NULL;
}
if (jOidSet != NULL) { if (jOidSet != NULL) {
for (i = 0; i < numOfOids; i++) { for (i = 0; i < numOfOids; i++) {
jOid = getJavaOID(env, &(cOidSet->elements[i])); jOid = getJavaOID(env, &(cOidSet->elements[i]));
@ -736,6 +758,7 @@ jobjectArray getJavaOIDArray(JNIEnv *env, gss_OID_set cOidSet) {
void debug(JNIEnv *env, char *msg) { void debug(JNIEnv *env, char *msg) {
jstring jmsg = (*env)->NewStringUTF(env, msg); jstring jmsg = (*env)->NewStringUTF(env, msg);
CHECK_NULL(jmsg);
(*env)->CallStaticVoidMethod(env, CLS_SunNativeProvider, (*env)->CallStaticVoidMethod(env, CLS_SunNativeProvider,
MID_SunNativeProvider_debug, jmsg); MID_SunNativeProvider_debug, jmsg);
(*env)->DeleteLocalRef(env, jmsg); (*env)->DeleteLocalRef(env, jmsg);

View File

@ -37,7 +37,7 @@ extern "C" {
extern OM_uint32 getGSSTime(jint); extern OM_uint32 getGSSTime(jint);
extern void checkStatus(JNIEnv *, jobject, OM_uint32, OM_uint32, char*); extern void checkStatus(JNIEnv *, jobject, OM_uint32, OM_uint32, char*);
extern jint checkTime(OM_uint32); extern jint checkTime(OM_uint32);
extern void initGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t); extern jint initGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t);
extern void resetGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t); extern void resetGSSBuffer(JNIEnv *, jbyteArray, gss_buffer_t);
extern gss_OID newGSSOID(JNIEnv *, jobject); extern gss_OID newGSSOID(JNIEnv *, jobject);