Skip to content

Commit aa4b84f

Browse files
committed
wolfcrypt/src/evp_pk.c: fix benign nullPointer in d2i_make_pkey() reported by cppcheck-2.20.0.
1 parent b3f08f3 commit aa4b84f

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

wolfcrypt/src/evp_pk.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ static int d2i_make_pkey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem,
6767

6868
/* Set the size and allocate memory for key data to be copied into. */
6969
pkey->pkey_sz = (int)memSz;
70-
pkey->pkey.ptr = (char*)XMALLOC((size_t)memSz, NULL,
71-
priv ? DYNAMIC_TYPE_PRIVATE_KEY : DYNAMIC_TYPE_PUBLIC_KEY);
72-
if (pkey->pkey.ptr == NULL) {
73-
ret = 0;
70+
if (memSz > 0) {
71+
pkey->pkey.ptr = (char*)XMALLOC((size_t)memSz, NULL,
72+
priv ? DYNAMIC_TYPE_PRIVATE_KEY : DYNAMIC_TYPE_PUBLIC_KEY);
73+
if (pkey->pkey.ptr == NULL) {
74+
ret = 0;
75+
}
76+
if (ret == 1) {
77+
/* Copy in key data. */
78+
XMEMCPY(pkey->pkey.ptr, mem, memSz);
79+
}
7480
}
7581
if (ret == 1) {
76-
/* Copy in key data, set key type passed in and return object. */
77-
XMEMCPY(pkey->pkey.ptr, mem, memSz);
82+
/* Set key type passed in and return object. */
7883
pkey->type = type;
7984
*out = pkey;
8085
}

0 commit comments

Comments
 (0)