Skip to content

Commit 6ee6608

Browse files
committed
fixes/workarounds for -Wnull-dereferences, some true positive, some false
positive: * src/pk.c:wolfSSL_RSA_meth_new() * tests/api.c:test_wolfSSL_PKCS7_certs() * tests/api.c:test_wolfSSL_X509V3_EXT_get() * wolfcrypt/src/asn.c:EncodeName() * wolfcrypt/src/pkcs12.c:wc_i2d_PKCS12() * wolfcrypt/src/port/af_alg/afalg_aes.c
1 parent 0727bae commit 6ee6608

5 files changed

Lines changed: 52 additions & 25 deletions

File tree

src/pk.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -970,23 +970,23 @@ WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags)
970970
int err;
971971

972972
/* Validate name is not NULL. */
973-
err = (name == NULL);
974-
if (!err) {
975-
/* Allocate an RSA METHOD to return. */
976-
meth = (WOLFSSL_RSA_METHOD*)XMALLOC(sizeof(WOLFSSL_RSA_METHOD), NULL,
977-
DYNAMIC_TYPE_OPENSSL);
978-
err = (meth == NULL);
979-
}
980-
if (!err) {
981-
XMEMSET(meth, 0, sizeof(*meth));
982-
meth->flags = flags;
983-
meth->dynamic = 1;
973+
if (name == NULL)
974+
return NULL;
975+
/* Allocate an RSA METHOD to return. */
976+
meth = (WOLFSSL_RSA_METHOD*)XMALLOC(sizeof(WOLFSSL_RSA_METHOD), NULL,
977+
DYNAMIC_TYPE_OPENSSL);
978+
if (meth == NULL)
979+
return NULL;
980+
981+
XMEMSET(meth, 0, sizeof(*meth));
982+
meth->flags = flags;
983+
meth->dynamic = 1;
984+
985+
name_len = (int)XSTRLEN(name);
986+
meth->name = (char*)XMALLOC((size_t)(name_len + 1), NULL,
987+
DYNAMIC_TYPE_OPENSSL);
988+
err = (meth->name == NULL);
984989

985-
name_len = (int)XSTRLEN(name);
986-
meth->name = (char*)XMALLOC((size_t)(name_len + 1), NULL,
987-
DYNAMIC_TYPE_OPENSSL);
988-
err = (meth->name == NULL);
989-
}
990990
if (!err) {
991991
XMEMCPY(meth->name, name, (size_t)(name_len + 1));
992992
}

tests/api.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20116,8 +20116,8 @@ static int test_wolfSSL_PKCS7_certs(void)
2011620116
while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
2011720117
X509_INFO* info = NULL;
2011820118
ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
20119-
ExpectIntGT(sk_X509_push(sk, info->x509), 0);
20120-
if (EXPECT_SUCCESS() && (info != NULL)) {
20119+
if (info != NULL) {
20120+
ExpectIntGT(sk_X509_push(sk, info->x509), 0);
2012120121
info->x509 = NULL;
2012220122
}
2012320123
X509_INFO_free(info);
@@ -32422,8 +32422,10 @@ static int test_wolfSSL_X509V3_EXT_get(void)
3242232422
ExpectIntNE((extNid = ext->obj->nid), NID_undef);
3242332423
ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
3242432424
ExpectIntEQ(method->ext_nid, extNid);
32425-
if (method->ext_nid == NID_subject_key_identifier) {
32426-
ExpectNotNull(method->i2s);
32425+
if (EXPECT_SUCCESS()) {
32426+
if (method->ext_nid == NID_subject_key_identifier) {
32427+
ExpectNotNull(method->i2s);
32428+
}
3242732429
}
3242832430
}
3242932431

wolfcrypt/src/asn.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29922,6 +29922,9 @@ static int EncodeName(EncodedName* name, const char* nameStr,
2992229922
name->used = 0;
2992329923
return 0;
2992429924
}
29925+
nameSz = (word32)cname->custom.valSz;
29926+
oid = cname->custom.oid;
29927+
oidSz = (word32)cname->custom.oidSz;
2992529928
}
2992629929
#else
2992729930
(void)cname;
@@ -29961,9 +29964,9 @@ static int EncodeName(EncodedName* name, const char* nameStr,
2996129964
break;
2996229965
#ifdef WOLFSSL_CUSTOM_OID
2996329966
case ASN_CUSTOM_NAME:
29964-
nameSz = (word32)cname->custom.valSz;
29965-
oid = cname->custom.oid;
29966-
oidSz = (word32)cname->custom.oidSz;
29967+
/* oid setup is above (mitigating false positive
29968+
* -Wnull-dereference).
29969+
*/
2996729970
break;
2996829971
#endif
2996929972
#ifdef WOLFSSL_CERT_REQ

wolfcrypt/src/pkcs12.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,10 @@ int wc_i2d_PKCS12(WC_PKCS12* pkcs12, byte** der, int* derSz)
977977
totalSz += seqSz;
978978

979979
/* check if getting length only */
980-
if (der == NULL && derSz != NULL) {
981-
*derSz = (int)totalSz;
980+
if (der == NULL) {
981+
/* repeat nullness check locally to mollify -Wnull-dereference. */
982+
if (derSz != NULL)
983+
*derSz = (int)totalSz;
982984
XFREE(sdBuf, pkcs12->heap, DYNAMIC_TYPE_PKCS);
983985
return WC_NO_ERR_TRACE(LENGTH_ONLY_E);
984986
}

wolfcrypt/src/port/af_alg/afalg_aes.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
186186
if ((sz / WC_AES_BLOCK_SIZE) > 0) {
187187
/* update IV */
188188
cmsg = CMSG_FIRSTHDR(&(aes->msg));
189+
if (cmsg == NULL) {
190+
WOLFSSL_MSG("CMSG_FIRSTHDR() in wc_AesCbcEncrypt() returned NULL unexpectedly.");
191+
return SYSLIB_FAILED_E;
192+
}
189193
ret = wc_Afalg_SetIv(CMSG_NXTHDR(&(aes->msg), cmsg),
190194
(byte*)(aes->reg), AES_IV_SIZE);
191195
if (ret < 0) {
@@ -245,6 +249,10 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
245249
if ((sz / WC_AES_BLOCK_SIZE) > 0) {
246250
/* update IV */
247251
cmsg = CMSG_FIRSTHDR(&(aes->msg));
252+
if (cmsg == NULL) {
253+
WOLFSSL_MSG("CMSG_FIRSTHDR() in wc_AesCbcDecrypt() returned NULL unexpectedly.");
254+
return SYSLIB_FAILED_E;
255+
}
248256
ret = wc_Afalg_SetIv(CMSG_NXTHDR(&(aes->msg), cmsg),
249257
(byte*)(aes->reg), AES_IV_SIZE);
250258
if (ret != 0) {
@@ -397,6 +405,10 @@ int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
397405

398406
/* update IV */
399407
cmsg = CMSG_FIRSTHDR(&(aes->msg));
408+
if (cmsg == NULL) {
409+
WOLFSSL_MSG("CMSG_FIRSTHDR() in wc_AesCtrEncrypt() returned NULL unexpectedly.");
410+
return SYSLIB_FAILED_E;
411+
}
400412
ret = wc_Afalg_SetIv(CMSG_NXTHDR(&(aes->msg), cmsg),
401413
(byte*)(aes->reg), AES_IV_SIZE);
402414
if (ret < 0) {
@@ -613,7 +625,15 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
613625

614626
msg = &(aes->msg);
615627
cmsg = CMSG_FIRSTHDR(msg);
628+
if (cmsg == NULL) {
629+
WOLFSSL_MSG("CMSG_FIRSTHDR() in wc_AesGcmEncrypt() returned NULL unexpectedly.");
630+
return SYSLIB_FAILED_E;
631+
}
616632
cmsg = CMSG_NXTHDR(msg, cmsg);
633+
if (cmsg == NULL) {
634+
WOLFSSL_MSG("CMSG_NEXTHDR() in wc_AesGcmEncrypt() returned NULL unexpectedly.");
635+
return SYSLIB_FAILED_E;
636+
}
617637

618638
/* set IV and AAD size */
619639
ret = wc_Afalg_SetIv(cmsg, (byte*)iv, ivSz);

0 commit comments

Comments
 (0)