Skip to content

Commit

Permalink
Handle NULL pointers in releaseNativeResources
Browse files Browse the repository at this point in the history
In the style of the previous commit, ensure all pointers are
non-NULL before continuing to free them. Some of these are excessive as
NSS does do some checking, but in this case consistency is better.

Signed-off-by: Alexander Scheel <[email protected]>
  • Loading branch information
cipherboy committed Apr 20, 2020
1 parent 1419fce commit f7b6acf
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 26 deletions.
2 changes: 1 addition & 1 deletion org/mozilla/jss/pkcs11/PK11Cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ JNIEXPORT void JNICALL
Java_org_mozilla_jss_pkcs11_CertProxy_releaseNativeResources
(JNIEnv *env, jobject this)
{
CERTCertificate *cert;
CERTCertificate *cert = NULL;
PRThread * VARIABLE_MAY_NOT_BE_USED pThread;

PR_ASSERT(env!=NULL && this!=NULL);
Expand Down
12 changes: 5 additions & 7 deletions org/mozilla/jss/pkcs11/PK11Module.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,14 @@ JNIEXPORT void JNICALL
Java_org_mozilla_jss_pkcs11_ModuleProxy_releaseNativeResources
(JNIEnv *env, jobject this)
{
SECMODModule *module;
SECMODModule *module = NULL;

if (JSS_getPtrFromProxy(env, this, (void **)&module) != PR_SUCCESS) {
ASSERT_OUTOFMEM(env);
goto finish;
return;
}
PR_ASSERT(module != NULL);

SECMOD_DestroyModule(module);

finish:
return;
if (module != NULL) {
SECMOD_DestroyModule(module);
}
}
2 changes: 1 addition & 1 deletion org/mozilla/jss/pkcs11/PK11PrivKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ JNIEXPORT void JNICALL
Java_org_mozilla_jss_pkcs11_PrivateKeyProxy_releaseNativeResources
(JNIEnv *env, jobject this)
{
SECKEYPrivateKey *privk;
SECKEYPrivateKey *privk = NULL;
PRThread * VARIABLE_MAY_NOT_BE_USED pThread;

PR_ASSERT(env!=NULL && this!=NULL);
Expand Down
19 changes: 7 additions & 12 deletions org/mozilla/jss/pkcs11/PK11Signature.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,18 +606,16 @@ JNIEXPORT void JNICALL
Java_org_mozilla_jss_pkcs11_SigContextProxy_releaseNativeResources
(JNIEnv *env, jobject this)
{
SigContextProxy *proxy;
SigContextProxy *proxy = NULL;

/* Retrieve the proxy pointer */
if( JSS_getPtrFromProxy(env, this, (void**)&proxy) != PR_SUCCESS) {
#ifdef DEBUG
PR_ASSERT( (*env)->ExceptionOccurred(env) != NULL);
PR_fprintf(PR_STDERR,
"ERROR: native signature context was not released\n");
#endif
goto finish;
if (JSS_getPtrFromProxy(env, this, (void**)&proxy) != PR_SUCCESS) {
return;
}

if (proxy == NULL) {
return;
}
PR_ASSERT(proxy!=NULL);

/* Free the context and the proxy */
if(proxy->type == SGN_CONTEXT) {
Expand All @@ -627,9 +625,6 @@ Java_org_mozilla_jss_pkcs11_SigContextProxy_releaseNativeResources
VFY_DestroyContext( (VFYContext*)proxy->ctxt, PR_TRUE /*freeit*/);
}
PR_Free(proxy);

finish:
;
}

/***********************************************************************
Expand Down
6 changes: 2 additions & 4 deletions org/mozilla/jss/pkcs11/PK11SymKey.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ Java_org_mozilla_jss_pkcs11_SymKeyProxy_releaseNativeResources

PR_ASSERT(env!=NULL && this!=NULL);

if( JSS_getPtrFromProxy(env, this, (void**)&key) == PR_SUCCESS) {
if (JSS_getPtrFromProxy(env, this, (void**)&key) == PR_SUCCESS &&
key != NULL) {
PK11_FreeSymKey(key);
} else {
/* can't really throw an exception from a destructor */
PR_ASSERT(PR_FALSE);
}
}
2 changes: 1 addition & 1 deletion org/mozilla/jss/pkcs11/PK11Token.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ JNIEXPORT void JNICALL
Java_org_mozilla_jss_pkcs11_TokenProxy_releaseNativeResources
(JNIEnv *env, jobject this)
{
PK11SlotInfo *slot;
PK11SlotInfo *slot = NULL;

PR_ASSERT(env!=NULL && this!=NULL);

Expand Down

0 comments on commit f7b6acf

Please sign in to comment.