Dealing with alarm issues in public infrastructure libraries

Signed-off-by: wang-jingwu001 <wangjingwu4@huawei.com>
https://gitee.com/openharmony/commonlibrary_ets_utils/issues/I9T4R8
This commit is contained in:
wang-jingwu001 2024-05-29 14:49:33 +08:00
parent 80c843045b
commit 7eb8a08aec
7 changed files with 47 additions and 33 deletions

View File

@ -220,6 +220,14 @@ static vector<uint8_t> 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<uint8_t> 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<void **>(&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<ParaType>(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<void **>(&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<uint8_t*>(data), byteOffset, length);
} else {
freeBufferMemory(buffer);
napi_throw_error(env, nullptr, "parameter type is error");
return nullptr;
}
return GetBufferWrapValue(env, thisVar, buffer);
}

View File

@ -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) {

View File

@ -110,10 +110,10 @@ public:
static std::vector<std::string> SplitNormalizedRecordName(const std::string &recordName)
{
std::vector<std::string> 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;

View File

@ -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);

View File

@ -333,8 +333,8 @@ void TaskManager::GetIdleWorkersList(uint32_t step)
if (workerWaitTime == 0) {
continue;
}
uint64_t currTime = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
uint64_t currTime = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::steady_clock::now().time_since_epoch()).count());
if (!isWorkerLoopActive) {
freeList_.emplace_back(worker);
} else if ((currTime - workerWaitTime) > 120) { // 120 : free after 120s

View File

@ -19,6 +19,7 @@
#include <vector>
#include <grp.h>
#include <mutex>
#include <pthread.h>
#include <pwd.h>
#include <sched.h>
@ -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<std::string, napi_ref> eventMap;
static std::mutex sharedTimedMutex;
thread_local std::map<napi_ref, napi_ref> 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<std::mutex> 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<std::mutex> lock(sharedTimedMutex);
iter.first = eventMap.erase(iter.first);
flag = true;
}

View File

@ -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<char*>(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<char*>(buffer.data()), buffer.length()) != EOK) {