diff --git a/js_api_module/buffer/native_module_buffer.cpp b/js_api_module/buffer/native_module_buffer.cpp index a4ec1ab7..5b0c8959 100644 --- a/js_api_module/buffer/native_module_buffer.cpp +++ b/js_api_module/buffer/native_module_buffer.cpp @@ -220,6 +220,14 @@ static vector GetArray(napi_env env, napi_value arr) return vec; } +static void freeBolbMemory(Blob *&blob) +{ + if (blob != nullptr) { + delete blob; + blob = nullptr; + } +} + static napi_value BlobConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -230,7 +238,6 @@ static napi_value BlobConstructor(napi_env env, napi_callback_info info) size_t argc = 3; napi_value argv[3] = { nullptr }; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - if (argc == 1) { // Array vector arr = GetArray(env, argv[0]); blob->Init(arr.data(), arr.size()); @@ -239,15 +246,14 @@ static napi_value BlobConstructor(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_unwrap(env, argv[0], reinterpret_cast(&blobIn))); int32_t start = -1; NAPI_CALL(env, napi_get_value_int32(env, argv[1], &start)); - // 2 : the argument's count is 2 - if (argc == 2) { + if (argc == 2) { // 2 : the argument's count is 2 blob->Init(blobIn, start); } else if (argc == 3) { // 3 : the argument's count is 3 int32_t end = -1; - // 2 : the third argument - NAPI_CALL(env, napi_get_value_int32(env, argv[2], &end)); + NAPI_CALL(env, napi_get_value_int32(env, argv[2], &end)); // 2 : the third argument blob->Init(blobIn, start, end); } else { + freeBolbMemory(blob); return nullptr; } } @@ -276,6 +282,14 @@ static napi_value GetBufferWrapValue(napi_env env, napi_value thisVar, Buffer *b return thisVar; } +static void freeBufferMemory(Buffer *&buffer) +{ + if (buffer != nullptr) { + delete buffer; + buffer = nullptr; + } +} + static napi_value BufferConstructor(napi_env env, napi_callback_info info) { napi_value thisVar = nullptr; @@ -283,10 +297,9 @@ static napi_value BufferConstructor(napi_env env, napi_callback_info info) if (buffer == nullptr) { return nullptr; } - size_t argc = 4; - napi_value argv[4] = { nullptr }; + size_t argc = 4; // 4:array size + napi_value argv[4] = { nullptr }; // 4:array size NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); - int32_t pType = -1; NAPI_CALL(env, napi_get_value_int32(env, argv[0], &pType)); ParaType paraType = static_cast(pType); @@ -307,16 +320,16 @@ static napi_value BufferConstructor(napi_env env, napi_callback_info info) Buffer *pool = nullptr; NAPI_CALL(env, napi_unwrap(env, argv[1], reinterpret_cast(&pool))); uint32_t poolOffset = 0; - // 2 : the third argument - NAPI_CALL(env, napi_get_value_uint32(env, argv[2], &poolOffset)); + NAPI_CALL(env, napi_get_value_uint32(env, argv[2], &poolOffset)); // 2 : the third argument uint32_t length = 0; - // 3 : the forth argument - NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &length)); + NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &length)); // 3 : the forth argument buffer->Init(pool, poolOffset, length); } else { + freeBufferMemory(buffer); return nullptr; } } else if (paraType == ParaType::STRING) { + freeBufferMemory(buffer); return nullptr; } else if (paraType == ParaType::UINT8ARRAY) { napi_typedarray_type type = napi_int8_array; @@ -330,20 +343,17 @@ static napi_value BufferConstructor(napi_env env, napi_callback_info info) } else if (paraType == ParaType::ARRAYBUFFER) { void *data = nullptr; size_t bufferSize = 0; - // 1 : the second argument - NAPI_CALL(env, napi_get_arraybuffer_info(env, argv[1], &data, &bufferSize)); + NAPI_CALL(env, napi_get_arraybuffer_info(env, argv[1], &data, &bufferSize)); // 1 : the second argument uint32_t byteOffset = 0; - // 2 : the third argument - NAPI_CALL(env, napi_get_value_uint32(env, argv[2], &byteOffset)); + NAPI_CALL(env, napi_get_value_uint32(env, argv[2], &byteOffset)); // 2 : the third argument uint32_t length = 0; - // 3 : the forth argument - NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &length)); + NAPI_CALL(env, napi_get_value_uint32(env, argv[3], &length)); // 3 : the forth argument buffer->Init(reinterpret_cast(data), byteOffset, length); } else { + freeBufferMemory(buffer); napi_throw_error(env, nullptr, "parameter type is error"); return nullptr; } - return GetBufferWrapValue(env, thisVar, buffer); } diff --git a/js_api_module/uri/js_uri.cpp b/js_api_module/uri/js_uri.cpp index 91e84fd0..bbdffa9e 100644 --- a/js_api_module/uri/js_uri.cpp +++ b/js_api_module/uri/js_uri.cpp @@ -412,8 +412,8 @@ namespace OHOS::Uri { bool Uri::IsHierarchical() const { - int index = inputUri_.find(':'); - if (index == -1) { + size_t index = inputUri_.find(':'); + if (index == std::string::npos) { return true; } if (inputUri_.length() == index + 1) { @@ -475,8 +475,8 @@ namespace OHOS::Uri { if (uriData_.path.empty()) { return segments; } - int previous = 0; - int current; + size_t previous = 0; + size_t current; for (current = uriData_.path.find('/', previous); current != std::string::npos; current = uriData_.path.find('/', previous)) { if (previous < current) { diff --git a/js_concurrent_module/common/helper/path_helper.h b/js_concurrent_module/common/helper/path_helper.h index fdba9b68..ce6a47fe 100644 --- a/js_concurrent_module/common/helper/path_helper.h +++ b/js_concurrent_module/common/helper/path_helper.h @@ -110,10 +110,10 @@ public: static std::vector SplitNormalizedRecordName(const std::string &recordName) { std::vector res(NORMALIZED_OHMURL_ARGS_NUM); - int index = NORMALIZED_OHMURL_ARGS_NUM - 1; + size_t index = NORMALIZED_OHMURL_ARGS_NUM - 1; std::string temp; - int endIndex = recordName.size() - 1; - for (int i = endIndex; i >= 0; i--) { + size_t endIndex = recordName.size() - 1; + for (size_t i = endIndex; i >= 0; i--) { char element = recordName[i]; if (element == NORMALIZED_OHMURL_TAG) { res[index] = temp; diff --git a/js_concurrent_module/taskpool/task.h b/js_concurrent_module/taskpool/task.h index f8dabc88..81d900ba 100644 --- a/js_concurrent_module/taskpool/task.h +++ b/js_concurrent_module/taskpool/task.h @@ -167,7 +167,7 @@ public: struct CallbackInfo { CallbackInfo(napi_env env, uint32_t count, napi_ref ref) - : hostEnv(env), refCount(count), callbackRef(ref), onCallbackSignal(nullptr) {} + : hostEnv(env), refCount(count), callbackRef(ref), onCallbackSignal(nullptr), worker(nullptr) {} ~CallbackInfo() { napi_delete_reference(hostEnv, callbackRef); diff --git a/js_concurrent_module/taskpool/task_manager.cpp b/js_concurrent_module/taskpool/task_manager.cpp index 723ba2dd..7551e83d 100644 --- a/js_concurrent_module/taskpool/task_manager.cpp +++ b/js_concurrent_module/taskpool/task_manager.cpp @@ -333,8 +333,8 @@ void TaskManager::GetIdleWorkersList(uint32_t step) if (workerWaitTime == 0) { continue; } - uint64_t currTime = std::chrono::duration_cast( - std::chrono::steady_clock::now().time_since_epoch()).count(); + uint64_t currTime = static_cast(std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count()); if (!isWorkerLoopActive) { freeList_.emplace_back(worker); } else if ((currTime - workerWaitTime) > 120) { // 120 : free after 120s diff --git a/js_sys_module/process/js_process.cpp b/js_sys_module/process/js_process.cpp index 5a15a339..dee7ef34 100644 --- a/js_sys_module/process/js_process.cpp +++ b/js_sys_module/process/js_process.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,7 @@ namespace OHOS::JsSysModule::Process { constexpr int FIRST_APPLICATION_UID = 10000; // 10000 : bundleId lower limit constexpr int LAST_APPLICATION_UID = 65535; // 65535 : bundleId upper limit thread_local std::multimap eventMap; + static std::mutex sharedTimedMutex; thread_local std::map pendingUnHandledRejections; // support events thread_local std::string events = "UnHandleRejection"; @@ -247,6 +249,7 @@ namespace OHOS::JsSysModule::Process { HILOG_ERROR("illegal event"); return; } + std::unique_lock lock(sharedTimedMutex); eventMap.insert(std::make_pair(result, myCallRef)); } } @@ -271,6 +274,7 @@ namespace OHOS::JsSysModule::Process { auto iter = eventMap.equal_range(temp); while (iter.first != iter.second) { NAPI_CALL(env, napi_delete_reference(env, iter.first->second)); + std::unique_lock lock(sharedTimedMutex); iter.first = eventMap.erase(iter.first); flag = true; } diff --git a/platform/ohos/util_helper.cpp b/platform/ohos/util_helper.cpp index d0b90509..8760f8ba 100644 --- a/platform/ohos/util_helper.cpp +++ b/platform/ohos/util_helper.cpp @@ -255,10 +255,10 @@ namespace Commonlibrary::Platform { std::string buffer = ""; std::u16string originalStr(originalBuffer, inputSize); - int shifting = 0; + size_t shifting = 0; size_t resultShifting = 0; - int findIndex = originalStr.find('\0'); - if (findIndex == -1) { + size_t findIndex = originalStr.find('\0'); + if (findIndex == std::string::npos) { buffer = UnicodeConversion(encoding, originalBuffer, inputSize); outLens = buffer.length(); if (memcpy_s(writeResult, outLens, reinterpret_cast(buffer.data()), outLens) != EOK) { @@ -266,7 +266,7 @@ namespace Commonlibrary::Platform { return; } } else { - while (findIndex != -1) { + while (findIndex != std::string::npos) { buffer = UnicodeConversion(encoding, originalBuffer + shifting, inputSize); if (memcpy_s(writeResult + resultShifting, buffer.length(), reinterpret_cast(buffer.data()), buffer.length()) != EOK) {