diff --git a/process/js_childprocess.cpp b/process/js_childprocess.cpp index 808eabc..26785a8 100644 --- a/process/js_childprocess.cpp +++ b/process/js_childprocess.cpp @@ -85,7 +85,7 @@ namespace OHOS::Js_sys_module::Process { close(stdOutFd_[0]); dup2(stdOutFd_[1], 1); dup2(stdErrFd_[1], 2); // 2:The value of parameter - if (execl("/bin/sh", "sh", "-c", strCommnd.c_str(), NULL) == -1) { + if (execl("/bin/sh", "sh", "-c", strCommnd.c_str(), nullptr) == -1) { HILOG_ERROR("execl command failed"); exit(127); // 127:The parameter value } @@ -247,7 +247,7 @@ namespace OHOS::Js_sys_module::Process { if (readSize >= 0) { stdOutInfo->stdData += childStdout; } - if (stdOutInfo->stdData.size() > stdOutInfo->maxBuffSize && *(stdOutInfo->isNeedRun)) { + if (stdOutInfo->stdData.size() > static_cast(stdOutInfo->maxBuffSize) && *(stdOutInfo->isNeedRun)) { if (!kill(stdOutInfo->pid, SIGKILL)) { *(stdOutInfo->isNeedRun) = false; stdOutInfo->stdData = stdOutInfo->stdData.substr(0, stdOutInfo->maxBuffSize); @@ -281,7 +281,7 @@ namespace OHOS::Js_sys_module::Process { if (readSize >= 0) { stdErrInfo->stdData += childStderr; } - if (stdErrInfo->stdData.size() > stdErrInfo->maxBuffSize && *(stdErrInfo->isNeedRun)) { + if (stdErrInfo->stdData.size() > static_cast(stdErrInfo->maxBuffSize) && *(stdErrInfo->isNeedRun)) { if (!kill(stdErrInfo->pid, SIGKILL)) { *(stdErrInfo->isNeedRun) = false; stdErrInfo->stdData = stdErrInfo->stdData.substr(0, stdErrInfo->maxBuffSize); @@ -393,7 +393,7 @@ namespace OHOS::Js_sys_module::Process { case 2: // 2:The parameter value status = napi_get_value_int64(env_, property, &optionsInfo_->maxBuffer); if (status != napi_ok) { - optionsInfo_->maxBuffer = MAXSIZE * MAXSIZE; + optionsInfo_->maxBuffer = static_cast(MAXSIZE) * static_cast(MAXSIZE); } break; default: @@ -405,22 +405,18 @@ namespace OHOS::Js_sys_module::Process { std::string ChildProcess::RequireStrValue(const napi_value strValue) { - char* buffer = nullptr; size_t bufferSize = 0; - - napi_get_value_string_utf8(env_, strValue, buffer, -1, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; + if (napi_get_value_string_utf8(env_, strValue, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get strValue size"); + return nullptr; } - - napi_get_value_string_utf8(env_, strValue, buffer, bufferSize + 1, &bufferSize); - - std::string result; - if (buffer != nullptr) { - result = buffer; + std::string result = ""; + result.reserve(bufferSize + 1); + result.resize(bufferSize); + if (napi_get_value_string_utf8(env_, strValue, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get strValue value"); + return nullptr; } - delete []buffer; - buffer = nullptr; return result; } diff --git a/process/js_process.cpp b/process/js_process.cpp index 54811d2..c89d0f9 100644 --- a/process/js_process.cpp +++ b/process/js_process.cpp @@ -128,23 +128,19 @@ namespace OHOS::Js_sys_module::Process { void Process::Chdir(napi_value args) const { size_t prolen = 0; - napi_get_value_string_utf8(env_, args, nullptr, 0, &prolen); - char* path = nullptr; - if (prolen > 0) { - path = new char[prolen + 1]; - if (memset_s(path, prolen + 1, '\0', prolen + 1) != EOK) { - napi_throw_error(env_, "-1", "chdir path memset_s failed"); - } - } else { - napi_throw_error(env_, "-2", "prolen is error !"); + if (napi_get_value_string_utf8(env_, args, nullptr, 0, &prolen) != napi_ok) { + HILOG_ERROR("can not get args size"); + return; + } + std::string result = ""; + result.reserve(prolen + 1); + result.resize(prolen); + if (napi_get_value_string_utf8(env_, args, result.data(), prolen + 1, &prolen) != napi_ok) { + HILOG_ERROR("can not get args value"); + return; } - napi_get_value_string_utf8(env_, args, path, prolen + 1, &prolen); int proerr = 0; - if (path != nullptr) { - proerr = uv_chdir(path); - delete []path; - path = nullptr; - } + proerr = uv_chdir(result.c_str()); if (proerr) { napi_throw_error(env_, "-1", "chdir"); } @@ -252,33 +248,30 @@ namespace OHOS::Js_sys_module::Process { napi_value Process::Off(napi_value str) { - char *buffer = nullptr; size_t bufferSize = 0; bool flag = false; - NAPI_CALL(env_, napi_get_value_string_utf8(env_, str, buffer, 0, &bufferSize)); - NAPI_ASSERT(env_, bufferSize > 0, "bufferSize == 0"); - buffer = new char[bufferSize + 1]; - if (memset_s(buffer, bufferSize + 1, 0, bufferSize + 1) != EOK) { - HILOG_ERROR("buffer memset error"); - delete []buffer; + if (napi_get_value_string_utf8(env_, str, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get str size"); return nullptr; } - napi_get_value_string_utf8(env_, str, buffer, bufferSize + 1, &bufferSize); - std::string temp = ""; - if (buffer != nullptr) { - temp = buffer; - delete []buffer; - buffer = nullptr; + std::string result = ""; + result.reserve(bufferSize + 1); + result.resize(bufferSize); + if (napi_get_value_string_utf8(env_, str, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get str value"); + return nullptr; } + std::string temp = ""; + temp = result; auto iter = eventMap.equal_range(temp); while (iter.first != iter.second) { NAPI_CALL(env_, napi_delete_reference(env_, iter.first->second)); iter.first = eventMap.erase(iter.first); flag = true; } - napi_value result = nullptr; - NAPI_CALL(env_, napi_get_boolean(env_, flag, &result)); - return result; + napi_value convertResult = nullptr; + NAPI_CALL(env_, napi_get_boolean(env_, flag, &convertResult)); + return convertResult; } napi_value Process::GetTid() const @@ -339,57 +332,59 @@ namespace OHOS::Js_sys_module::Process { napi_value Process::GetEnvironmentVar(napi_value name) const { - char *buffer = nullptr; - napi_value result = nullptr; + napi_value convertResult = nullptr; char buf[260 * NUM_OF_DATA] = { 0 }; // 260:Only numbers path String size is 260. size_t length = sizeof(buf); size_t bufferSize = 0; - napi_get_value_string_utf8(env_, name, buffer, 0, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; + if (napi_get_value_string_utf8(env_, name, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get name size"); + return nullptr; + } + std::string result = ""; + result.reserve(bufferSize + 1); + result.resize(bufferSize); + if (napi_get_value_string_utf8(env_, name, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get name value"); + return nullptr; } - napi_get_value_string_utf8(env_, name, buffer, bufferSize + 1, &bufferSize); std::string temp = ""; - if (buffer != nullptr) { - temp = buffer; - delete []buffer; - buffer = nullptr; - } + temp = result; auto envNum = uv_os_getenv(temp.c_str(), buf, &length); if (envNum == UV_ENOENT) { - NAPI_CALL(env_, napi_get_undefined(env_, &result)); - return result; + NAPI_CALL(env_, napi_get_undefined(env_, &convertResult)); + return convertResult; } - napi_create_string_utf8(env_, buf, strlen(buf), &result); - return result; + napi_create_string_utf8(env_, buf, strlen(buf), &convertResult); + return convertResult; } napi_value Process::GetUidForName(napi_value name) const { struct passwd *user = nullptr; int32_t uid = 0; - napi_value result = nullptr; - char *buffer = nullptr; + napi_value convertResult = nullptr; size_t bufferSize = 0; - napi_get_value_string_utf8(env_, name, buffer, 0, &bufferSize); - if (bufferSize > 0) { - buffer = new char[bufferSize + 1]; + if (napi_get_value_string_utf8(env_, name, nullptr, 0, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get name size"); + return nullptr; + } + std::string result = ""; + result.reserve(bufferSize + 1); + result.resize(bufferSize); + if (napi_get_value_string_utf8(env_, name, result.data(), bufferSize + 1, &bufferSize) != napi_ok) { + HILOG_ERROR("can not get name value"); + return nullptr; } - napi_get_value_string_utf8(env_, name, buffer, bufferSize + 1, &bufferSize); std::string temp = ""; - if (buffer != nullptr) { - temp = buffer; - delete []buffer; - buffer = nullptr; - } + temp = result; user = getpwnam(temp.c_str()); if (user != nullptr) { uid = static_cast(user->pw_uid); - napi_create_int32(env_, uid, &result); - return result; + napi_create_int32(env_, uid, &convertResult); + return convertResult; } - napi_create_int32(env_, (-1), &result); - return result; + napi_create_int32(env_, (-1), &convertResult); + return convertResult; } napi_value Process::GetThreadPriority(napi_value tid) const diff --git a/process/native_module_process.cpp b/process/native_module_process.cpp index 9eef170..c8d0039 100644 --- a/process/native_module_process.cpp +++ b/process/native_module_process.cpp @@ -13,12 +13,11 @@ * limitations under the License. */ +#include #include - #include #include #include - #include "js_childprocess.h" #include "js_process.h" #include "securec.h" @@ -261,43 +260,43 @@ namespace OHOS::Js_sys_module::Process { return result; } - static napi_value GetUid(napi_env env, napi_callback_info info) + static napi_value GetUid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetUid(); } - static napi_value GetGid(napi_env env, napi_callback_info info) + static napi_value GetGid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetGid(); } - static napi_value GetEUid(napi_env env, napi_callback_info info) + static napi_value GetEUid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetEUid(); } - static napi_value GetEGid(napi_env env, napi_callback_info info) + static napi_value GetEGid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetEGid(); } - static napi_value GetGroups(napi_env env, napi_callback_info info) + static napi_value GetGroups(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetGroups(); } - static napi_value GetPid(napi_env env, napi_callback_info info) + static napi_value GetPid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetPid(); } - static napi_value GetPpid(napi_env env, napi_callback_info info) + static napi_value GetPpid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetPpid(); @@ -321,7 +320,7 @@ namespace OHOS::Js_sys_module::Process { return result; } - static napi_value Abort(napi_env env, napi_callback_info info) + static napi_value Abort(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); object.Abort(); @@ -330,7 +329,7 @@ namespace OHOS::Js_sys_module::Process { return res; } - static napi_value Cwd(napi_env env, napi_callback_info info) + static napi_value Cwd(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.Cwd(); @@ -387,7 +386,7 @@ namespace OHOS::Js_sys_module::Process { return result; } - static napi_value Uptime(napi_env env, napi_callback_info info) + static napi_value Uptime(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.Uptime(); @@ -405,13 +404,13 @@ namespace OHOS::Js_sys_module::Process { result = object.Kill(argv[0], argv[1]); return result; } - static napi_value GetTid(napi_env env, napi_callback_info info) + static napi_value GetTid(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetTid(); } - static napi_value IsIsolatedProcess(napi_env env, napi_callback_info info) + static napi_value IsIsolatedProcess(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.IsIsolatedProcess(); @@ -430,7 +429,7 @@ namespace OHOS::Js_sys_module::Process { return object.IsAppUid(args); } - static napi_value Is64Bit(napi_env env, napi_callback_info info) + static napi_value Is64Bit(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.Is64Bit(); @@ -462,13 +461,13 @@ namespace OHOS::Js_sys_module::Process { return object.GetThreadPriority(args); } - static napi_value GetStartRealtime(napi_env env, napi_callback_info info) + static napi_value GetStartRealtime(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetStartRealtime(); } - static napi_value GetPastCputime(napi_env env, napi_callback_info info) + static napi_value GetPastCputime(napi_env env, [[maybe_unused]] napi_callback_info info) { Process object(env); return object.GetPastCputime();