-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix NULL Checks and Error Handling #527
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,19 +67,23 @@ static void oqs_kem_freectx(void *vpkemctx) { | |
PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx; | ||
|
||
OQS_KEM_PRINTF("OQS KEM provider called: freectx\n"); | ||
oqsx_key_free(pkemctx->kem); | ||
OPENSSL_free(pkemctx); | ||
if (pkemctx != NULL) { | ||
oqsx_key_free(pkemctx->kem); | ||
OPENSSL_free(pkemctx); | ||
} | ||
} | ||
|
||
static int oqs_kem_decapsencaps_init(void *vpkemctx, void *vkem, | ||
int operation) { | ||
PROV_OQSKEM_CTX *pkemctx = (PROV_OQSKEM_CTX *)vpkemctx; | ||
|
||
OQS_KEM_PRINTF3("OQS KEM provider called: _init : New: %p; old: %p \n", | ||
vkem, pkemctx->kem); | ||
vkem, pkemctx ? pkemctx->kem : NULL); | ||
if (pkemctx == NULL || vkem == NULL || !oqsx_key_up_ref(vkem)) | ||
return 0; | ||
oqsx_key_free(pkemctx->kem); | ||
if (pkemctx->kem != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's nothing technically wrong with putting this guard here, but it's not necessary— There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the comments. |
||
oqsx_key_free(pkemctx->kem); | ||
} | ||
pkemctx->kem = vkem; | ||
|
||
return 1; | ||
|
@@ -106,13 +110,13 @@ static int oqs_qs_kem_encaps_keyslot(void *vpkemctx, unsigned char *out, | |
const OQS_KEM *kem_ctx = NULL; | ||
|
||
OQS_KEM_PRINTF("OQS KEM provider called: encaps\n"); | ||
if (pkemctx->kem == NULL) { | ||
if (pkemctx == NULL || pkemctx->kem == NULL) { | ||
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n"); | ||
return -1; | ||
} | ||
|
||
kem_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem; | ||
if (pkemctx->kem->comp_pubkey == NULL || | ||
if (kem_ctx == NULL || pkemctx->kem->comp_pubkey == NULL || | ||
pkemctx->kem->comp_pubkey[keyslot] == NULL) { | ||
OQS_KEM_PRINTF("OQS Warning: public key is NULL\n"); | ||
return -1; | ||
|
@@ -156,12 +160,12 @@ static int oqs_qs_kem_decaps_keyslot(void *vpkemctx, unsigned char *out, | |
const OQS_KEM *kem_ctx = NULL; | ||
|
||
OQS_KEM_PRINTF("OQS KEM provider called: decaps\n"); | ||
if (pkemctx->kem == NULL) { | ||
if (pkemctx == NULL || pkemctx->kem == NULL) { | ||
OQS_KEM_PRINTF("OQS Warning: OQS_KEM not initialized\n"); | ||
return -1; | ||
} | ||
kem_ctx = pkemctx->kem->oqsx_provider_ctx.oqsx_qs_ctx.kem; | ||
if (pkemctx->kem->comp_privkey == NULL || | ||
if (kem_ctx == NULL || pkemctx->kem->comp_privkey == NULL || | ||
pkemctx->kem->comp_privkey[keyslot] == NULL) { | ||
OQS_KEM_PRINTF("OQS Warning: private key is NULL\n"); | ||
return -1; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as below:
OPENSSL_free
handles NULL gracefully, and imo it's cleaner to not perform the check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The NULL check is for prevent from NULL pointer for oqsx_key_free(pkemctx->kem), not for OPENSSL_free/free().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I should have been more clear: I meant to write that the
OPENSSL_free
call could be moved outside the conditional block, not that the check itself could be removed entirely. Certainly it's still necessary before the dereference.