From aa4b84f9a23d108519786958146854ce213af56d Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Mon, 9 Mar 2026 10:58:36 -0500 Subject: [PATCH] wolfcrypt/src/evp_pk.c: fix benign nullPointer in d2i_make_pkey() reported by cppcheck-2.20.0. --- wolfcrypt/src/evp_pk.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/src/evp_pk.c b/wolfcrypt/src/evp_pk.c index d5f4911b02..bf0b3cf6af 100644 --- a/wolfcrypt/src/evp_pk.c +++ b/wolfcrypt/src/evp_pk.c @@ -67,14 +67,19 @@ static int d2i_make_pkey(WOLFSSL_EVP_PKEY** out, const unsigned char* mem, /* Set the size and allocate memory for key data to be copied into. */ pkey->pkey_sz = (int)memSz; - pkey->pkey.ptr = (char*)XMALLOC((size_t)memSz, NULL, - priv ? DYNAMIC_TYPE_PRIVATE_KEY : DYNAMIC_TYPE_PUBLIC_KEY); - if (pkey->pkey.ptr == NULL) { - ret = 0; + if (memSz > 0) { + pkey->pkey.ptr = (char*)XMALLOC((size_t)memSz, NULL, + priv ? DYNAMIC_TYPE_PRIVATE_KEY : DYNAMIC_TYPE_PUBLIC_KEY); + if (pkey->pkey.ptr == NULL) { + ret = 0; + } + if (ret == 1) { + /* Copy in key data. */ + XMEMCPY(pkey->pkey.ptr, mem, memSz); + } } if (ret == 1) { - /* Copy in key data, set key type passed in and return object. */ - XMEMCPY(pkey->pkey.ptr, mem, memSz); + /* Set key type passed in and return object. */ pkey->type = type; *out = pkey; }