diff --git a/frameworks/js/napi/crypto/src/napi_mac.cpp b/frameworks/js/napi/crypto/src/napi_mac.cpp index 353e1f590b3dc26493070c33d721451445a0e74f..e66670ce3092b3a8e33c63e5099693019412f47d 100644 --- a/frameworks/js/napi/crypto/src/napi_mac.cpp +++ b/frameworks/js/napi/crypto/src/napi_mac.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -64,13 +64,19 @@ static void FreeCryptoFwkCtx(napi_env env, MacCtx *context) HcfFree(context->inBlob->data); context->inBlob->data = nullptr; context->inBlob->len = 0; + HcfFree(context->inBlob); + context->inBlob = nullptr; } if (context->outBlob != nullptr) { HcfFree(context->outBlob->data); context->outBlob->data = nullptr; context->outBlob->len = 0; + HcfFree(context->outBlob); + context->outBlob = nullptr; } + context->errMsg = nullptr; HcfFree(context); + context = nullptr; } static void ReturnCallbackResult(napi_env env, MacCtx *context, napi_value result) @@ -194,6 +200,8 @@ static void MacDoFinalExecute(napi_env env, void *data) if (context->errCode != HCF_SUCCESS) { LOGE("doFinal failed!"); context->errMsg = "doFinal failed"; + HcfFree(outBlob); + outBlob = nullptr; return; } context->outBlob = outBlob; @@ -282,6 +290,7 @@ napi_value NapiMac::MacUpdate(napi_env env, napi_callback_info info) if (context->inBlob == nullptr) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null")); LOGE("inBlob is null!"); + FreeCryptoFwkCtx(env, context); return nullptr; } if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) { diff --git a/frameworks/js/napi/crypto/src/napi_md.cpp b/frameworks/js/napi/crypto/src/napi_md.cpp index 1416a703a934f3839dc0b1224d4fd57d7602408a..060e2b9ac5201512ca6de75f941434df3dc33754 100644 --- a/frameworks/js/napi/crypto/src/napi_md.cpp +++ b/frameworks/js/napi/crypto/src/napi_md.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -62,13 +62,19 @@ static void FreeCryptoFwkCtx(napi_env env, MdCtx *context) HcfFree(context->inBlob->data); context->inBlob->data = nullptr; context->inBlob->len = 0; + HcfFree(context->inBlob); + context->inBlob = nullptr; } if (context->outBlob != nullptr) { HcfFree(context->outBlob->data); context->outBlob->data = nullptr; context->outBlob->len = 0; + HcfFree(context->outBlob); + context->outBlob = nullptr; } + context->errMsg = nullptr; HcfFree(context); + context = nullptr; } static void ReturnCallbackResult(napi_env env, MdCtx *context, napi_value result) @@ -163,6 +169,8 @@ static void MdDoFinalExecute(napi_env env, void *data) if (context->errCode != HCF_SUCCESS) { LOGE("doFinal failed!"); context->errMsg = "doFinal failed"; + HcfFree(outBlob); + outBlob = nullptr; return; } context->outBlob = outBlob; @@ -205,6 +213,7 @@ napi_value NapiMd::MdUpdate(napi_env env, napi_callback_info info) if (context->inBlob == nullptr) { napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "inBlob is null")); LOGE("inBlob is null!"); + FreeCryptoFwkCtx(env, context); return nullptr; } if (!CreateCallbackAndPromise(env, context, argc, ARGS_SIZE_TWO, argv[PARAM1])) { diff --git a/frameworks/js/napi/crypto/src/napi_rand.cpp b/frameworks/js/napi/crypto/src/napi_rand.cpp index e20aa98e197f607d4c513df7b72bf175b03e8d61..2ee120962bd7263ed407cf04c71eae77e301e1fd 100644 --- a/frameworks/js/napi/crypto/src/napi_rand.cpp +++ b/frameworks/js/napi/crypto/src/napi_rand.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (C) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -62,13 +62,19 @@ static void FreeCryptoFwkCtx(napi_env env, RandCtx *context) HcfFree(context->seedBlob->data); context->seedBlob->data = nullptr; context->seedBlob->len = 0; + HcfFree(context->seedBlob); + context->seedBlob = nullptr; } if (context->randBlob != nullptr) { HcfFree(context->randBlob->data); context->randBlob->data = nullptr; context->randBlob->len = 0; + HcfFree(context->randBlob); + context->randBlob = nullptr; } + context->errMsg = nullptr; HcfFree(context); + context = nullptr; } static void ReturnCallbackResult(napi_env env, RandCtx *context, napi_value result) @@ -140,6 +146,8 @@ static void GenerateRandomExecute(napi_env env, void *data) if (context->errCode != HCF_SUCCESS) { LOGE("generateRandom failed!"); context->errMsg = "generateRandom failed"; + HcfFree(randBlob); + randBlob = nullptr; return; } context->randBlob = randBlob;