From 82f252a2ed523d654277b55126719c3fc5281348 Mon Sep 17 00:00:00 2001 From: "@lixingyang-li" Date: Tue, 20 Aug 2024 17:26:09 +0800 Subject: [PATCH] =?UTF-8?q?=C2=B4=C2=A6=C3=80=C2=BE=C2=B2=CC=AC=C2=B8?= =?UTF-8?q?=E6=BE=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: @lixingyang-li https://gitee.com/openharmony/commonlibrary_ets_utils/issues/IALA70 --- js_api_module/buffer/native_module_buffer.cpp | 28 ++++-- js_api_module/url/native_module_url.cpp | 43 +++++++++- .../collections/native_module_collections.cpp | 2 +- js_util_module/util/js_base64.cpp | 28 ++++-- js_util_module/util/js_stringdecoder.cpp | 2 +- js_util_module/util/js_stringdecoder.h | 2 +- js_util_module/util/js_textdecoder.cpp | 16 +++- js_util_module/util/js_textdecoder.h | 2 +- js_util_module/util/js_uuid.cpp | 4 +- js_util_module/util/native_module_util.cpp | 86 +++++++++++++++---- platform/ohos/util_helper.cpp | 12 +++ 11 files changed, 183 insertions(+), 42 deletions(-) diff --git a/js_api_module/buffer/native_module_buffer.cpp b/js_api_module/buffer/native_module_buffer.cpp index 06c3d878..59850ece 100644 --- a/js_api_module/buffer/native_module_buffer.cpp +++ b/js_api_module/buffer/native_module_buffer.cpp @@ -213,8 +213,8 @@ static vector GetArray(napi_env env, napi_value arr) vector vec; for (size_t i = 0; i < length; i++) { napi_get_element(env, arr, i, &napiNumber); - int32_t num = 0; - napi_get_value_int32(env, napiNumber, &num); + uint32_t num = 0; + napi_get_value_uint32(env, napiNumber, &num); // 255 : the max number of one byte unsigned value num = num & 0xFF; vec.push_back(num); @@ -233,7 +233,7 @@ static void freeBolbMemory(Blob *&blob) static napi_value BlobConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; - Blob *blob = new Blob(); + Blob *blob = new (std::nothrow) Blob(); if (blob == nullptr) { return nullptr; } @@ -351,7 +351,11 @@ static bool InitAnyArrayBuffer(napi_env env, napi_value* argv, Buffer *&buffer) static Buffer* BufferConstructorInner(napi_env env, size_t argc, napi_value* argv, ParaType paraType) { - Buffer *buffer = new Buffer(); + Buffer *buffer = new (std::nothrow) Buffer(); + if (buffer == nullptr) { + HILOG_ERROR("BufferStructor:: buffer is nullptr"); + return nullptr; + } uint32_t length = 0; if (paraType == ParaType::NUMBER) { if (napi_get_value_uint32(env, argv[1], &length) != napi_ok) { @@ -643,9 +647,9 @@ static napi_value GetBufferData(napi_env env, napi_callback_info info) Buffer *buf = nullptr; NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&buf))); uint32_t length = buf->GetLength(); - uint8_t* data = new uint8_t[length]; + uint8_t* data = new (std::nothrow) uint8_t[length]; if (data == nullptr) { - HILOG_ERROR("buffer:: data is NULL"); + HILOG_ERROR("buffer:: data is nullptr"); return result; } buf->ReadBytes(data, 0, length); @@ -1051,7 +1055,11 @@ static void CopiedBlobToArrayBuffer(napi_env env, napi_status status, void *data static napi_value ArrayBufferAsync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; - PromiseInfo *promiseInfo = new PromiseInfo(); + PromiseInfo *promiseInfo = new (std::nothrow) PromiseInfo(); + if (promiseInfo == nullptr) { + HILOG_ERROR("buffer:: promiseInfo is nullptr"); + return nullptr; + } napi_value resourceName = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Blob *blob = nullptr; @@ -1074,7 +1082,11 @@ static napi_value ArrayBufferAsync(napi_env env, napi_callback_info info) static napi_value TextAsync(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; - PromiseInfo *promiseInfo = new PromiseInfo(); + PromiseInfo *promiseInfo = new (std::nothrow) PromiseInfo(); + if (promiseInfo == nullptr) { + HILOG_ERROR("buffer:: promiseInfo is nullptr"); + return nullptr; + } napi_value resourceName = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Blob *blob = nullptr; diff --git a/js_api_module/url/native_module_url.cpp b/js_api_module/url/native_module_url.cpp index 375cdbee..0a49119e 100644 --- a/js_api_module/url/native_module_url.cpp +++ b/js_api_module/url/native_module_url.cpp @@ -67,11 +67,23 @@ namespace OHOS::Url { return; } std::string base = tempType; - object = new URL(input, base); + object = new (std::nothrow) URL(input, base); + if (object == nullptr) { + HILOG_ERROR("UrlStructor:: object is nullptr"); + return; + } } else if (valuetype2 == napi_object) { URL *tempUrl = nullptr; napi_unwrap(env, argv[1], reinterpret_cast(&tempUrl)); - object = new URL(input, *tempUrl); + if (tempUrl == nullptr) { + HILOG_ERROR("UrlStructor:: tempUrl is nullptr"); + return; + } + object = new (std::nothrow) URL(input, *tempUrl); + if (object == nullptr) { + HILOG_ERROR("UrlStructor:: object is nullptr"); + return; + } } else { HILOG_INFO("secondParameter error"); } @@ -563,6 +575,9 @@ namespace OHOS::Url { } URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } napi_value result = object->Get(env, args); return result; } @@ -579,6 +594,9 @@ namespace OHOS::Url { } URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } napi_value result = object->GetAll(env, args); return result; } @@ -596,6 +614,9 @@ namespace OHOS::Url { } URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } object->Append(env, args[0], args[1]); return nullptr; } @@ -612,6 +633,9 @@ namespace OHOS::Url { } URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } object->Delete(env, args); return nullptr; } @@ -624,6 +648,9 @@ namespace OHOS::Url { napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } napi_value result = object->Entries(env); return result; } @@ -648,6 +675,9 @@ namespace OHOS::Url { napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } object->Set(env, args[0], args[1]); return nullptr; } @@ -660,6 +690,9 @@ namespace OHOS::Url { napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } object->Sort(); return nullptr; } @@ -672,6 +705,9 @@ namespace OHOS::Url { napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } napi_value result = object->IterByKeys(env); return result; } @@ -684,6 +720,9 @@ namespace OHOS::Url { napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; napi_unwrap(env, thisVar, reinterpret_cast(&object)); + if (object == nullptr) { + return nullptr; + } napi_value result = object->IterByValues(env); return result; } diff --git a/js_util_module/collections/native_module_collections.cpp b/js_util_module/collections/native_module_collections.cpp index 59326980..4faebc34 100644 --- a/js_util_module/collections/native_module_collections.cpp +++ b/js_util_module/collections/native_module_collections.cpp @@ -36,7 +36,7 @@ const std::string SENDABLE_UINT8_CLAMPED_ARRAY = "SendableUint8ClampedArray"; const std::string SENDABLE_FLOAT32_ARRAY = "SendableFloat32Array"; } // namespace -static bool GetCollectionFunction(napi_env env, napi_value global, std::string collectionName, +static bool GetCollectionFunction(napi_env env, napi_value global, const std::string collectionName, napi_value &collectionFunction) { napi_value collectionKey; diff --git a/js_util_module/util/js_base64.cpp b/js_util_module/util/js_base64.cpp index c21f3b37..31515f5e 100644 --- a/js_util_module/util/js_base64.cpp +++ b/js_util_module/util/js_base64.cpp @@ -111,7 +111,11 @@ namespace OHOS::Util { outputLen += TRAGET_FOUR; } if (outputLen > 0) { - ret = new unsigned char[outputLen + 1]; + ret = new (std::nothrow) unsigned char[outputLen + 1]; + if (ret == nullptr) { + HILOG_ERROR("Base64:: ret is nullptr"); + return nullptr; + } if (memset_s(ret, outputLen + 1, '\0', outputLen + 1) != EOK) { HILOG_ERROR("encode ret memset_s failed"); FreeMemory(ret); @@ -130,9 +134,7 @@ namespace OHOS::Util { flag = true; } const char *searchArray = flag ? BASEURL : BASE; - unsigned char *result = nullptr; - result = EncodeAchieveInner(input, ret, searchArray, inputLen, valueType); - + unsigned char *result = EncodeAchieveInner(input, ret, searchArray, inputLen, valueType); return result; } @@ -207,7 +209,11 @@ namespace OHOS::Util { size_t prolen = 0; napi_get_value_string_utf8(env, src, nullptr, 0, &prolen); if (prolen > 0) { - inputString = new char[prolen + 1]; + inputString = new (std::nothrow) char[prolen + 1]; + if (inputString == nullptr) { + HILOG_ERROR("inputString is nullptr"); + return false; + } if (memset_s(inputString, prolen + 1, '\0', prolen + 1) != EOK) { FreeMemory(inputString); napi_throw_error(env, "-1", "decode inputString memset_s failed"); @@ -248,7 +254,11 @@ namespace OHOS::Util { } retLen = DecodeOut(equalCount, retLen); if (retLen > 0) { - retDecode = new unsigned char[retLen + 1]; + retDecode = new (std::nothrow) unsigned char[retLen + 1]; + if (retDecode == nullptr) { + HILOG_ERROR("retDecode is nullptr"); + return nullptr; + } if (memset_s(retDecode, retLen + 1, '\0', retLen + 1) != EOK) { FreeMemory(retDecode); napi_throw_error(env, "-1", "decode retDecode memset_s failed"); @@ -388,7 +398,11 @@ namespace OHOS::Util { void Base64::CreateEncodePromise(napi_env env, unsigned char *inputDecode, size_t length, Type valueType) { napi_value resourceName = nullptr; - stdEncodeInfo_ = new EncodeInfo(); + stdEncodeInfo_ = new (std::nothrow) EncodeInfo(); + if (stdEncodeInfo_ == nullptr) { + HILOG_ERROR("stdEncodeInfo_ is nullptr"); + return; + } stdEncodeInfo_->sinputEncode = inputDecode; stdEncodeInfo_->slength = length; stdEncodeInfo_->env = env; diff --git a/js_util_module/util/js_stringdecoder.cpp b/js_util_module/util/js_stringdecoder.cpp index 0f6ece2c..81025673 100644 --- a/js_util_module/util/js_stringdecoder.cpp +++ b/js_util_module/util/js_stringdecoder.cpp @@ -101,7 +101,7 @@ napi_value StringDecoder::End(napi_env env) return resultStr; } -void StringDecoder::FreedMemory(UChar *pData) +void StringDecoder::FreedMemory(UChar *&pData) { if (pData != nullptr) { delete[] pData; diff --git a/js_util_module/util/js_stringdecoder.h b/js_util_module/util/js_stringdecoder.h index 92abbd44..e168f8ce 100644 --- a/js_util_module/util/js_stringdecoder.h +++ b/js_util_module/util/js_stringdecoder.h @@ -30,7 +30,7 @@ public: napi_value End(napi_env env, napi_value src); napi_value End(napi_env env); private: - void FreedMemory(UChar *pData); + void FreedMemory(UChar *&pData); const char* pend_ {}; int pendingLen_ {}; UConverter* conv_ {}; diff --git a/js_util_module/util/js_textdecoder.cpp b/js_util_module/util/js_textdecoder.cpp index 81ea2d8c..05663ce7 100644 --- a/js_util_module/util/js_textdecoder.cpp +++ b/js_util_module/util/js_textdecoder.cpp @@ -72,7 +72,11 @@ namespace OHOS::Util { size_t len = limit * sizeof(UChar); UChar *arr = nullptr; if (limit > 0) { - arr = new UChar[limit + 1]; + arr = new (std::nothrow) UChar[limit + 1]; + if (arr == nullptr) { + HILOG_ERROR("decode arr is nullptr"); + return nullptr; + } if (memset_s(arr, len + sizeof(UChar), 0, len + sizeof(UChar)) != EOK) { HILOG_ERROR("decode arr memset_s failed"); FreedMemory(arr); @@ -125,7 +129,11 @@ namespace OHOS::Util { size_t len = limit * sizeof(UChar); UChar *arr = nullptr; if (limit > 0) { - arr = new UChar[limit + 1]{0}; + arr = new (std::nothrow) UChar[limit + 1]{0}; + if (arr == nullptr) { + HILOG_DEBUG("arr is nullptr"); + return nullptr; + } } else { HILOG_DEBUG("limit is error"); return nullptr; @@ -159,7 +167,7 @@ namespace OHOS::Util { napi_value TextDecoder::GetEncoding(napi_env env) const { - size_t length = strlen(encStr_.c_str()); + size_t length = encStr_.length(); napi_value result = nullptr; NAPI_CALL(env, napi_create_string_utf8(env, encStr_.c_str(), length, &result)); return result; @@ -210,7 +218,7 @@ namespace OHOS::Util { ucnv_reset(tranTool_.get()); } - void TextDecoder::FreedMemory(UChar *pData) + void TextDecoder::FreedMemory(UChar *&pData) { if (pData != nullptr) { delete[] pData; diff --git a/js_util_module/util/js_textdecoder.h b/js_util_module/util/js_textdecoder.h index 6edc2d74..18cc3b50 100644 --- a/js_util_module/util/js_textdecoder.h +++ b/js_util_module/util/js_textdecoder.h @@ -150,7 +150,7 @@ namespace OHOS::Util { void SetBomFlag(const UChar *arr, const UErrorCode codeFlag, const DecodeArr decArr, size_t& rstLen, bool& bomFlag); void SetIgnoreBOM(const UChar *arr, size_t resultLen, bool& bomFlag); - void FreedMemory(UChar *pData); + void FreedMemory(UChar *&pData); const char* ReplaceNull(void *data, size_t length) const; napi_value ThrowError(napi_env env, const char* errMessage); uint32_t label_ {}; diff --git a/js_util_module/util/js_uuid.cpp b/js_util_module/util/js_uuid.cpp index ebef7302..55ca26e3 100644 --- a/js_util_module/util/js_uuid.cpp +++ b/js_util_module/util/js_uuid.cpp @@ -181,7 +181,9 @@ std::string GetFormatUUID(const UUID &uuid) napi_value GetBinaryUUID(napi_env env, bool entropyCache) { UUID uuid; - GetUUID(env, entropyCache, uuid); + if (!GetUUID(env, entropyCache, uuid)) { + return nullptr; + } void *data = nullptr; napi_value arrayBuffer = nullptr; size_t bufferSize = sizeof(uuid.elements); diff --git a/js_util_module/util/native_module_util.cpp b/js_util_module/util/native_module_util.cpp index cb873cb0..5a4cd4e8 100644 --- a/js_util_module/util/native_module_util.cpp +++ b/js_util_module/util/native_module_util.cpp @@ -66,7 +66,11 @@ namespace OHOS::Util { if (length == 0) { return nullptr; } - char *type = new char[length + 1]; + char *type = new (std::nothrow) char[length + 1]; + if (type == nullptr) { + HILOG_ERROR("type is nullptr"); + return nullptr; + } if (memset_s(type, length + 1, '\0', length + 1) != EOK) { HILOG_ERROR("type memset_s failed"); delete[] type; @@ -165,7 +169,9 @@ namespace OHOS::Util { i++; } } - res = res.substr(0, res.size() - 1); + if (!res.empty()) { + res = res.substr(0, res.size() - 1); + } napi_value result = nullptr; napi_create_string_utf8(env, res.c_str(), res.size(), &result); return result; @@ -180,10 +186,14 @@ namespace OHOS::Util { static napi_value DealWithFormatString(napi_env env, napi_callback_info info) { size_t argc = 1; + size_t requireArgc = 1; // 1:The number of parameters is 1 napi_value argv = nullptr; napi_get_cb_info(env, info, &argc, 0, nullptr, nullptr); napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr); + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } std::string format = ""; size_t formatsize = 0; if (napi_get_value_string_utf8(env, argv, nullptr, 0, &formatsize) != napi_ok) { @@ -201,8 +211,7 @@ namespace OHOS::Util { static std::string PrintfString(const std::string &format, const std::vector &value) { - std::string printInfo = DealWithPrintf(format, value); - return printInfo; + return DealWithPrintf(format, value); } static napi_value Printf(napi_env env, napi_callback_info info) @@ -212,7 +221,11 @@ namespace OHOS::Util { napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); napi_value *argv = nullptr; if (argc > 0) { - argv = new napi_value[argc]; + argv = new (std::nothrow) napi_value[argc]; + if (argv == nullptr) { + HILOG_ERROR("Printf:: argv is nullptr"); + return nullptr; + } napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); std::string format = ""; size_t formatsize = 0; @@ -427,7 +440,11 @@ namespace OHOS::Util { } delete []type; type = nullptr; - auto objectInfo = new TextDecoder(enconding, paraVec); + auto objectInfo = new (std::nothrow) TextDecoder(enconding, paraVec); + if (objectInfo == nullptr) { + HILOG_ERROR("TextDecoder objectInfo is nullptr"); + return nullptr; + } NAPI_CALL(env, napi_wrap( env, thisVar, objectInfo, [](napi_env environment, void *data, void *hint) { @@ -455,6 +472,10 @@ namespace OHOS::Util { bool iStream = false; TextDecoder *textDecoder = nullptr; napi_unwrap(env, thisVar, (void**)&textDecoder); + if (textDecoder == nullptr) { + HILOG_ERROR("DecodeToString::textDecoder is nullptr"); + return nullptr; + } napi_value valStr = nullptr; if (tempArgc == 1) { argc = 1; @@ -504,6 +525,9 @@ namespace OHOS::Util { bool iStream = false; TextDecoder *textDecoder = nullptr; napi_unwrap(env, thisVar, (void**)&textDecoder); + if (textDecoder == nullptr) { + return nullptr; + } napi_value valStr = nullptr; if (tempArgc == 1) { argc = 1; @@ -621,7 +645,11 @@ namespace OHOS::Util { encoding = buffer; } } - auto object = new TextEncoder(encoding); + auto object = new (std::nothrow) TextEncoder(encoding); + if (object == nullptr) { + HILOG_ERROR("TextEncoder:: object is nullptr"); + return nullptr; + } object->SetOrgEncoding(orgEncoding); object->SetApiIsolated(env); napi_wrap( @@ -738,9 +766,13 @@ namespace OHOS::Util { static napi_value EncodeIntoUint8Array(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; + size_t requireArgc = 2; // 2:The number of parameters is 2 size_t argc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; // 2:The number of parameters is 2 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } napi_valuetype valuetype0; NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); napi_typedarray_type valuetype1; @@ -767,7 +799,7 @@ namespace OHOS::Util { size_t argc = 0; napi_value args[2] = { nullptr }; // 2:The number of parameters is 2 napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); - if (argc == 2 || argc >= 2) { // 2:The number of parameters is 2 + if (argc >= 2) { // 2:The number of parameters is 2 return EncodeIntoTwo(env, info); } return EncodeIntoOne(env, info); @@ -993,10 +1025,14 @@ namespace OHOS::Util { static napi_value EncodeToStringHelper(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 2; // 2:The number of parameters is 2 + size_t requireArgc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } int32_t encode = 0; NAPI_CALL(env, napi_get_value_int32(env, args[1], &encode)); Type typeValue = static_cast(encode); @@ -1011,10 +1047,14 @@ namespace OHOS::Util { static napi_value EncodeBase64Helper(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 2; // 2:The number of parameters is 2 + size_t requireArgc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } int32_t encode = 0; NAPI_CALL(env, napi_get_value_int32(env, args[1], &encode)); Type typeValue = static_cast(encode); @@ -1028,11 +1068,14 @@ namespace OHOS::Util { static napi_value EncodeAsyncHelper(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 2; // 2:The number of parameters is 2 + size_t requireArgc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); - + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } int32_t encode = 0; NAPI_CALL(env, napi_get_value_int32(env, args[1], &encode)); Type typeValue = static_cast(encode); @@ -1043,10 +1086,14 @@ namespace OHOS::Util { static napi_value EncodeToStringAsyncHelper(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 2; // 2:The number of parameters is 2 + size_t requireArgc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } int32_t encode = 0; NAPI_CALL(env, napi_get_value_int32(env, args[1], &encode)); Type typeValue = static_cast(encode); @@ -1058,11 +1105,14 @@ namespace OHOS::Util { static napi_value DecodeBase64Helper(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 2; // 2:The number of parameters is 2 + size_t requireArgc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); - + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } int32_t encode = 0; NAPI_CALL(env, napi_get_value_int32(env, args[1], &encode)); Type typeValue = static_cast(encode); @@ -1077,10 +1127,14 @@ namespace OHOS::Util { static napi_value DecodeAsyncHelper(napi_env env, napi_callback_info info) { - size_t argc = 2; + size_t argc = 2; // 2:The number of parameters is 2 + size_t requireArgc = 2; // 2:The number of parameters is 2 napi_value args[2] = { nullptr }; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr)); + if (argc < requireArgc) { + return ThrowError(env, "Parameter error. Wrong number of arguments."); + } int32_t encode = 0; NAPI_CALL(env, napi_get_value_int32(env, args[1], &encode)); Type typeValue = static_cast(encode); diff --git a/platform/ohos/util_helper.cpp b/platform/ohos/util_helper.cpp index d6829607..c0ceaf00 100644 --- a/platform/ohos/util_helper.cpp +++ b/platform/ohos/util_helper.cpp @@ -258,6 +258,10 @@ namespace Commonlibrary::Platform { outLen = static_cast(maxByteSize) * inputSize; napi_create_arraybuffer(env, outLen, &data, arrayBuffer); char *writeResult = static_cast(data); + if (writeResult == nullptr) { + HILOG_ERROR("textencoder::writeResult is nullptr"); + return; + } std::string buffer = ""; std::u16string originalStr(originalBuffer, inputSize); size_t shifting = 0; @@ -320,6 +324,10 @@ namespace Commonlibrary::Platform { size_t inputSize = 0; napi_get_value_string_utf16(encodeInfo.env, encodeInfo.src, nullptr, 0, &inputSize); char16_t *originalBuffer = ApplyMemory(inputSize); + if (originalBuffer == nullptr) { + HILOG_ERROR("textencoder::originalBuffer is nullptr"); + return; + } napi_get_value_string_utf16(encodeInfo.env, encodeInfo.src, originalBuffer, inputSize + 1, &inputSize); std::vector targetBuffer(inputSize + 1, u'\0'); @@ -352,6 +360,10 @@ namespace Commonlibrary::Platform { size_t inputSize = 0; napi_get_value_string_utf16(encodeInfo.env, encodeInfo.src, nullptr, 0, &inputSize); char16_t *originalBuffer = ApplyMemory(inputSize); + if (originalBuffer == nullptr) { + HILOG_ERROR("textencoder::originalBuffer is nullptr"); + return; + } napi_get_value_string_utf16(encodeInfo.env, encodeInfo.src, originalBuffer, inputSize + 1, &inputSize); size_t writableSize = length;