From 4cf2180803c397a4362c86ec1f371afe1490be79 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Tue, 9 May 2023 14:56:47 +0800 Subject: [PATCH 1/9] =?UTF-8?q?fileApi=E8=87=AA=E6=B5=8B=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E4=BF=AE=E5=A4=8D.=20Signed-off-by:=20yangji?= =?UTF-8?q?ngbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: If3a81b44d1ad58a7240bfa73407a6950dc4719d0 --- interfaces/kits/js/src/mod_fs/common_func.cpp | 3 +-- interfaces/kits/js/src/mod_fs/properties/movedir.cpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index f5f999312..b3c289196 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -87,7 +87,6 @@ static tuple GetActualLen(napi_env env, size_t bufLen, size_t bufO unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) { - static constexpr unsigned int usrReadOnly = 00; static constexpr unsigned int usrWriteOnly = 01; static constexpr unsigned int usrReadWrite = 02; static constexpr unsigned int usrCreate = 0100; @@ -99,8 +98,8 @@ unsigned int CommonFunc::ConvertJsFlags(unsigned int &flags) static constexpr unsigned int usrNoFollowed = 0400000; static constexpr unsigned int usrSynchronous = 04010000; + // default value is usrReadOnly 00 unsigned int flagsABI = 0; - flagsABI |= ((flags & usrReadOnly) == usrReadOnly) ? O_RDONLY : 0; flagsABI |= ((flags & usrWriteOnly) == usrWriteOnly) ? O_WRONLY : 0; flagsABI |= ((flags & usrReadWrite) == usrReadWrite) ? O_RDWR : 0; flagsABI |= ((flags & usrCreate) == usrCreate) ? O_CREAT : 0; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 5a2fcd1a5..d5a05df59 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -318,8 +318,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - auto arg = make_shared(); + auto arg = std::shared_ptr(new (std::nothrow) MoveDirArgs()); if (arg == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); -- Gitee From ce816e56b9d8db0b6db76d6eacc69c63fe7027d2 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Wed, 10 May 2023 09:16:38 +0800 Subject: [PATCH 2/9] =?UTF-8?q?make=E5=88=9B=E5=BB=BA=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=8E=BB=E9=99=A4=E5=88=A4=E7=A9=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20Signed-off-by:=20yangjingbo10=20=20=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I2fe8ce08bf352df3cb61974dd7b83f46688e4aa6 --- .../src/mod_file/class_file/file_n_exporter.cpp | 8 -------- .../js/src/mod_fileio/properties/read_text.cpp | 10 ---------- .../src/mod_fs/class_stream/stream_n_exporter.cpp | 10 ---------- .../kits/js/src/mod_fs/properties/copy_file.cpp | 8 -------- .../kits/js/src/mod_fs/properties/copydir.cpp | 5 ----- .../js/src/mod_fs/properties/create_stream.cpp | 5 ----- .../js/src/mod_fs/properties/fdopen_stream.cpp | 5 ----- .../kits/js/src/mod_fs/properties/listfile.cpp | 5 ----- .../kits/js/src/mod_fs/properties/movedir.cpp | 7 +------ .../js/src/mod_fs/properties/prop_n_exporter.cpp | 15 --------------- interfaces/kits/js/src/mod_fs/properties/stat.cpp | 5 ----- .../kits/js/src/mod_fs/properties/truncate.cpp | 5 ----- .../js/src/mod_securitylabel/security_label.h | 3 --- utils/filemgmt_libn/src/n_val.cpp | 4 ---- 14 files changed, 1 insertion(+), 94 deletions(-) diff --git a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp index c7db1223a..3b195879d 100644 --- a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp @@ -846,10 +846,6 @@ void ReadTextExec(napi_env env, void *data) int result = stat(path.c_str(), &buf); if (fdg.GetFD() != FAILED && result != FAILED) { auto buffer = std::make_unique(buf.st_size + 1); - if (buffer == nullptr) { - UniError(ENOMEM).ThrowErr(env); - return; - } if (read(fdg.GetFD(), buffer.get(), buf.st_size) != FAILED) { asyncCallbackInfo->result = SUCCESS; asyncCallbackInfo->contents = std::string(buffer.get()); @@ -897,10 +893,6 @@ void ReadArrayBufferExec(napi_env env, void *data) int32_t len = (asyncCallbackInfo->length == COMMON_NUM::ZERO) ? (buf.st_size - begin) : asyncCallbackInfo->length; auto buffer = std::make_unique(len + 1); - if (buffer == nullptr) { - UniError(ENOMEM).ThrowErr(env); - return; - } lseek(fdg.GetFD(), begin, SEEK_CUR); if (read(fdg.GetFD(), buffer.get(), len) != FAILED) { asyncCallbackInfo->result = SUCCESS; diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp index afd1135f5..feb3f9819 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp @@ -109,10 +109,6 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) len = !hasLen ? statbf.st_size : len; len = ((len < statbf.st_size) ? len : statbf.st_size); std::unique_ptr readbuf = std::make_unique(len + 1); - if (readbuf == nullptr) { - UniError(EINVAL).ThrowErr(env, "file is too large"); - return nullptr; - } if (memset_s(readbuf.get(), len + 1, 0, len + 1) != EOK) { UniError(errno).ThrowErr(env, "dfs mem error"); @@ -157,9 +153,6 @@ UniError ReadText::AsyncExec(const std::string &path, std::shared_ptrlen = ((arg->len < statbf.st_size) ? arg->len : statbf.st_size); arg->buf = std::make_unique(arg->len); - if (arg->buf == nullptr) { - return UniError(ENOMEM); - } if (position > 0) { arg->len = pread(sfd.GetFD(), arg->buf.get(), arg->len, position); @@ -195,9 +188,6 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(NVal(env, funcArg.GetThisVar())); - if (arg == nullptr) { - return nullptr; - } auto cbExec = [path = string(path.get()), arg, position = position, hasLen = hasLen, len = len](napi_env env) -> UniError { diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 5bfd00d7a..771ad42af 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -170,11 +170,6 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) } auto arg = make_shared(move(bufGuard)); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); @@ -236,11 +231,6 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) } auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 51744a991..7387ac868 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -81,10 +81,6 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) return NError(errno); } srcFile.fdg = make_unique(ret, true); - if (!srcFile.fdg) { - HILOGE("Failed to request heap memory for src file descriptor."); - return NError(ENOMEM); - } } struct stat statbf; @@ -106,10 +102,6 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) return NError(errno); } destFile.fdg = make_unique(ret, true); - if (!destFile.fdg) { - HILOGE("Failed to request heap memory for dest file descriptor."); - return NError(ENOMEM); - } } return SendFileCore(srcFile, destFile, statbf); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 32ab2fe4b..9675747f1 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -278,11 +278,6 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (arg == nullptr) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = CopyDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 5ea0beb50..7e6667d9e 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -86,11 +86,6 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError { arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index eee88be73..0e8296274 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -86,11 +86,6 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, fd = fd, mode = mode]() -> NError { arg->fp = { fdopen(fd, mode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 6468c964c..836cde17c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -352,11 +352,6 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, optionArgsTmp]() -> NError { g_optionArgs = optionArgsTmp; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index d5a05df59..146b0a558 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -318,12 +318,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto arg = std::shared_ptr(new (std::nothrow) MoveDirArgs()); - if (arg == nullptr) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } + auto arg = make_shared(); auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = MoveDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index f9c145ac9..c9aee181c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -108,11 +108,6 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) } auto result = make_shared(); - if (!result) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [path = string(tmp.get()), result]() -> NError { std::unique_ptr access_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -399,11 +394,6 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) } auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return ReadExec(arg, static_cast(buf), len, fd, offset); @@ -476,11 +466,6 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) } auto arg = make_shared(move(bufGuard)); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return WriteExec(arg, static_cast(buf), len, fd, offset); diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index d68e7cfd2..56ed01920 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -40,11 +40,6 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { false, FileInfo { false, {}, {} } }; } auto fdg = make_unique(fd, false); - if (!fdg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; - } return { true, FileInfo { false, {}, move(fdg) } }; } HILOGE("Invalid parameter"); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index a67ce8911..5fdf2aaa8 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -39,11 +39,6 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { false, FileInfo { false, {}, {} } }; } auto fdg = make_unique(fd, false); - if (!fdg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; - } return { true, FileInfo { false, {}, move(fdg) } }; }; diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h index e789b7261..d50feee89 100644 --- a/interfaces/kits/js/src/mod_securitylabel/security_label.h +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -51,9 +51,6 @@ public: return DEFAULT_DATA_LEVEL; } std::unique_ptr xattrValue = std::make_unique((long)xattrValueSize + 1); - if (xattrValue == nullptr) { - return ""; - } xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue.get(), xattrValueSize); if (xattrValueSize == -1 || errno == ENOTSUP) { diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 5e2944006..7341ae157 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -76,10 +76,6 @@ tuple, size_t> NVal::ToUTF8String(string defaultValue) { if (TypeIs(napi_undefined) || TypeIs(napi_function)) { unique_ptr str = make_unique(defaultValue.size() + 1); - if (str == nullptr) { - HILOGE("Failed to request heap memory"); - return { false, nullptr, 0 }; - } copy(defaultValue.begin(), defaultValue.end(), str.get()); str[defaultValue.size()] = '\0'; return make_tuple(true, move(str), defaultValue.size()); -- Gitee From 952bf363c392bf259d4ca9ef2d46613828b9cf06 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Wed, 10 May 2023 09:16:38 +0800 Subject: [PATCH 3/9] =?UTF-8?q?make=E5=88=9B=E5=BB=BA=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E6=8C=87=E9=92=88=E5=8E=BB=E9=99=A4=E5=88=A4=E7=A9=BA=E9=80=BB?= =?UTF-8?q?=E8=BE=91=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I2fe8ce08bf352df3cb61974dd7b83f46688e4aa6 --- .../src/mod_file/class_file/file_n_exporter.cpp | 8 -------- .../js/src/mod_fileio/properties/read_text.cpp | 11 ----------- .../src/mod_fs/class_stream/stream_n_exporter.cpp | 10 ---------- .../kits/js/src/mod_fs/properties/copy_file.cpp | 8 -------- .../kits/js/src/mod_fs/properties/copydir.cpp | 5 ----- .../js/src/mod_fs/properties/create_stream.cpp | 5 ----- .../js/src/mod_fs/properties/fdopen_stream.cpp | 5 ----- .../kits/js/src/mod_fs/properties/listfile.cpp | 5 ----- .../kits/js/src/mod_fs/properties/movedir.cpp | 7 +------ .../js/src/mod_fs/properties/prop_n_exporter.cpp | 15 --------------- interfaces/kits/js/src/mod_fs/properties/stat.cpp | 5 ----- .../kits/js/src/mod_fs/properties/truncate.cpp | 5 ----- .../js/src/mod_securitylabel/security_label.h | 3 --- utils/filemgmt_libn/src/n_val.cpp | 4 ---- 14 files changed, 1 insertion(+), 95 deletions(-) diff --git a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp index c7db1223a..3b195879d 100644 --- a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp @@ -846,10 +846,6 @@ void ReadTextExec(napi_env env, void *data) int result = stat(path.c_str(), &buf); if (fdg.GetFD() != FAILED && result != FAILED) { auto buffer = std::make_unique(buf.st_size + 1); - if (buffer == nullptr) { - UniError(ENOMEM).ThrowErr(env); - return; - } if (read(fdg.GetFD(), buffer.get(), buf.st_size) != FAILED) { asyncCallbackInfo->result = SUCCESS; asyncCallbackInfo->contents = std::string(buffer.get()); @@ -897,10 +893,6 @@ void ReadArrayBufferExec(napi_env env, void *data) int32_t len = (asyncCallbackInfo->length == COMMON_NUM::ZERO) ? (buf.st_size - begin) : asyncCallbackInfo->length; auto buffer = std::make_unique(len + 1); - if (buffer == nullptr) { - UniError(ENOMEM).ThrowErr(env); - return; - } lseek(fdg.GetFD(), begin, SEEK_CUR); if (read(fdg.GetFD(), buffer.get(), len) != FAILED) { asyncCallbackInfo->result = SUCCESS; diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp index afd1135f5..eeea99ce8 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp @@ -109,11 +109,6 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) len = !hasLen ? statbf.st_size : len; len = ((len < statbf.st_size) ? len : statbf.st_size); std::unique_ptr readbuf = std::make_unique(len + 1); - if (readbuf == nullptr) { - UniError(EINVAL).ThrowErr(env, "file is too large"); - return nullptr; - } - if (memset_s(readbuf.get(), len + 1, 0, len + 1) != EOK) { UniError(errno).ThrowErr(env, "dfs mem error"); return nullptr; @@ -157,9 +152,6 @@ UniError ReadText::AsyncExec(const std::string &path, std::shared_ptrlen = ((arg->len < statbf.st_size) ? arg->len : statbf.st_size); arg->buf = std::make_unique(arg->len); - if (arg->buf == nullptr) { - return UniError(ENOMEM); - } if (position > 0) { arg->len = pread(sfd.GetFD(), arg->buf.get(), arg->len, position); @@ -195,9 +187,6 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(NVal(env, funcArg.GetThisVar())); - if (arg == nullptr) { - return nullptr; - } auto cbExec = [path = string(path.get()), arg, position = position, hasLen = hasLen, len = len](napi_env env) -> UniError { diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 5bfd00d7a..771ad42af 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -170,11 +170,6 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) } auto arg = make_shared(move(bufGuard)); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); @@ -236,11 +231,6 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) } auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 51744a991..7387ac868 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -81,10 +81,6 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) return NError(errno); } srcFile.fdg = make_unique(ret, true); - if (!srcFile.fdg) { - HILOGE("Failed to request heap memory for src file descriptor."); - return NError(ENOMEM); - } } struct stat statbf; @@ -106,10 +102,6 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) return NError(errno); } destFile.fdg = make_unique(ret, true); - if (!destFile.fdg) { - HILOGE("Failed to request heap memory for dest file descriptor."); - return NError(ENOMEM); - } } return SendFileCore(srcFile, destFile, statbf); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 32ab2fe4b..9675747f1 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -278,11 +278,6 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (arg == nullptr) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = CopyDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 5ea0beb50..7e6667d9e 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -86,11 +86,6 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError { arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index eee88be73..0e8296274 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -86,11 +86,6 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, fd = fd, mode = mode]() -> NError { arg->fp = { fdopen(fd, mode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 6468c964c..836cde17c 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -352,11 +352,6 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, optionArgsTmp]() -> NError { g_optionArgs = optionArgsTmp; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index d5a05df59..146b0a558 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -318,12 +318,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto arg = std::shared_ptr(new (std::nothrow) MoveDirArgs()); - if (arg == nullptr) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } + auto arg = make_shared(); auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = MoveDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index f9c145ac9..c9aee181c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -108,11 +108,6 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) } auto result = make_shared(); - if (!result) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [path = string(tmp.get()), result]() -> NError { std::unique_ptr access_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -399,11 +394,6 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) } auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return ReadExec(arg, static_cast(buf), len, fd, offset); @@ -476,11 +466,6 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) } auto arg = make_shared(move(bufGuard)); - if (!arg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return nullptr; - } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return WriteExec(arg, static_cast(buf), len, fd, offset); diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index d68e7cfd2..56ed01920 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -40,11 +40,6 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { false, FileInfo { false, {}, {} } }; } auto fdg = make_unique(fd, false); - if (!fdg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; - } return { true, FileInfo { false, {}, move(fdg) } }; } HILOGE("Invalid parameter"); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index a67ce8911..5fdf2aaa8 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -39,11 +39,6 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { false, FileInfo { false, {}, {} } }; } auto fdg = make_unique(fd, false); - if (!fdg) { - HILOGE("Failed to request heap memory."); - NError(ENOMEM).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; - } return { true, FileInfo { false, {}, move(fdg) } }; }; diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h index e789b7261..d50feee89 100644 --- a/interfaces/kits/js/src/mod_securitylabel/security_label.h +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -51,9 +51,6 @@ public: return DEFAULT_DATA_LEVEL; } std::unique_ptr xattrValue = std::make_unique((long)xattrValueSize + 1); - if (xattrValue == nullptr) { - return ""; - } xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue.get(), xattrValueSize); if (xattrValueSize == -1 || errno == ENOTSUP) { diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 5e2944006..7341ae157 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -76,10 +76,6 @@ tuple, size_t> NVal::ToUTF8String(string defaultValue) { if (TypeIs(napi_undefined) || TypeIs(napi_function)) { unique_ptr str = make_unique(defaultValue.size() + 1); - if (str == nullptr) { - HILOGE("Failed to request heap memory"); - return { false, nullptr, 0 }; - } copy(defaultValue.begin(), defaultValue.end(), str.get()); str[defaultValue.size()] = '\0'; return make_tuple(true, move(str), defaultValue.size()); -- Gitee From edfb3bcc77a5c1fe506c426b516185f8c9f574ef Mon Sep 17 00:00:00 2001 From: y30045862 Date: Fri, 12 May 2023 09:15:46 +0800 Subject: [PATCH 4/9] =?UTF-8?q?make=E6=99=BA=E8=83=BD=E6=8C=87=E6=AD=A3?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0try-catch=E6=8D=95=E8=8E=B7=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=88=86=E9=85=8D=E5=BC=82=E5=B8=B8=20Signed-off-by:=20yangjin?= =?UTF-8?q?gbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I21d7106ecf5343930ad1ef1d27f453289e9e50bc --- interfaces/kits/js/BUILD.gn | 2 ++ .../src/mod_fs/class_file/file_n_exporter.cpp | 9 ++++- .../src/mod_fs/class_stat/stat_n_exporter.cpp | 9 ++++- .../mod_fs/class_stream/stream_n_exporter.cpp | 27 +++++++++++++-- .../class_watcher/watcher_n_exporter.cpp | 9 ++++- .../js/src/mod_fs/properties/copy_file.cpp | 33 ++++++++++++++++--- .../kits/js/src/mod_fs/properties/copydir.cpp | 9 ++++- .../src/mod_fs/properties/create_stream.cpp | 9 ++++- .../src/mod_fs/properties/fdopen_stream.cpp | 9 ++++- .../js/src/mod_fs/properties/listfile.cpp | 10 ++++-- .../kits/js/src/mod_fs/properties/lstat.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/movedir.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/open.cpp | 18 ++++++++-- .../src/mod_fs/properties/prop_n_exporter.cpp | 29 +++++++++++++--- .../js/src/mod_fs/properties/read_text.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/stat.cpp | 18 ++++++++-- .../js/src/mod_fs/properties/truncate.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/watcher.cpp | 17 ++++++++-- 19 files changed, 222 insertions(+), 31 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e1b4ec79a..a3474936c 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -163,6 +163,8 @@ ohos_shared_library("fs") { "ipc:ipc_core", "samgr:samgr_proxy", ] + + use_exceptions = true } ohos_shared_library("hash") { diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 4ab935a24..f80b8f74d 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -195,7 +195,14 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - auto rafEntity = make_unique(); + unique_ptr rafEntity; + try { + rafEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(rafEntity))) { HILOGE("Failed to set file entity"); NError(EIO).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index 0b2baa572..214d06611 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -234,7 +234,14 @@ napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr statEntity = make_unique(); + unique_ptr statEntity; + try { + statEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(statEntity))) { HILOGE("Failed to set stat entity"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 771ad42af..3f765f420 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -169,7 +169,14 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = make_shared(move(bufGuard)); + shared_ptr arg; + try { + arg = make_shared(move(bufGuard)); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); @@ -230,7 +237,14 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); + shared_ptr arg; + try { + arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); @@ -319,7 +333,14 @@ napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) return nullptr; } - unique_ptr streamEntity = make_unique(); + unique_ptr streamEntity; + try { + streamEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(streamEntity))) { HILOGE("INNER BUG. Failed to wrap entity for obj stream"); NError(EIO).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index abc578626..6faaa9d9e 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -37,7 +37,14 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr watcherEntity = make_unique(); + unique_ptr watcherEntity; + try { + watcherEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { NError(EIO).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 7387ac868..0548aaa79 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -80,7 +80,12 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); } - srcFile.fdg = make_unique(ret, true); + try { + srcFile.fdg = make_unique(ret, true); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } } struct stat statbf; @@ -101,7 +106,12 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); } - destFile.fdg = make_unique(ret, true); + try { + destFile.fdg = make_unique(ret, true); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } } return SendFileCore(srcFile, destFile, statbf); } @@ -128,7 +138,14 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; } @@ -199,7 +216,15 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [para = make_shared(move(src), move(dest))]() -> NError { + shared_ptr para; + try { + para = make_shared(move(src), move(dest)); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [para]() -> NError { if (para->src_.isPath && para->dest_.isPath) { return IsAllPath(para->src_, para->dest_); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 9675747f1..765490999 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -277,7 +277,14 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = CopyDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 7e6667d9e..81123841e 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -85,7 +85,14 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError { arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index 0e8296274..bcfd5bde5 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -85,7 +85,14 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, fd = fd, mode = mode]() -> NError { arg->fp = { fdopen(fd, mode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 836cde17c..bb841933d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -351,8 +351,14 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); - + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, optionArgsTmp]() -> NError { g_optionArgs = optionArgsTmp; if (g_optionArgs.recursion) { diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 1391bba8a..30f2751b2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -77,7 +77,14 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) } string path = tmp.get(); - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, path]() -> NError { std::unique_ptr lstat_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 532e78180..7dafca9cc 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -74,7 +74,14 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(tmp.get()), arg]() -> NError { std::unique_ptr mkdtemp_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 146b0a558..0847f28cd 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -318,7 +318,14 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = MoveDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 684a310f3..bc3554fb1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -100,7 +100,14 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } return NVal(); } - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return NVal(); + } fileEntity->fd_.swap(fdg); if (isUri) { fileEntity->path_ = ""; @@ -266,7 +273,14 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index c9aee181c..a3da8820c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -107,7 +107,14 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } - auto result = make_shared(); + shared_ptr result; + try { + result = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(tmp.get()), result]() -> NError { std::unique_ptr access_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -393,8 +400,14 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - + shared_ptr arg; + try { + arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return ReadExec(arg, static_cast(buf), len, fd, offset); }; @@ -465,8 +478,14 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(move(bufGuard)); - + shared_ptr arg; + try { + arg = make_shared(move(bufGuard)); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return WriteExec(arg, static_cast(buf), len, fd, offset); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 0436ae4bd..0a6e57411 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -211,7 +211,14 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(NVal(env, funcArg.GetThisVar())); + shared_ptr arg; + try { + arg = make_shared(NVal(env, funcArg.GetThisVar())); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(path.get()), arg, offset = offset, hasLen = hasLen, len = len]() -> NError { return ReadTextAsync(path, arg, offset, hasLen, len); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 56ed01920..ba98585ac 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -39,7 +39,14 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; } HILOGE("Invalid parameter"); @@ -100,7 +107,14 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { std::unique_ptr stat_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 5fdf2aaa8..ec72bf29e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -38,7 +38,14 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; }; diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 45e72c264..ef4eeda4d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -64,7 +64,14 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - shared_ptr watcherPtr = make_shared(); + shared_ptr watcherPtr; + try { + watcherPtr = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!watcherPtr->InitNotify(fd)) { NError(errno).ThrowErr(env); return nullptr; @@ -87,7 +94,13 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return nullptr; } - watcherEntity->data_ = std::make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + try { + watcherEntity->data_ = make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } watcherEntity->data_->events = events; watcherEntity->data_->env = env; watcherEntity->data_->filename = string(filename.get()); -- Gitee From 8c268fbed79beb727d12c7425837334154f6bed4 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Fri, 12 May 2023 09:15:46 +0800 Subject: [PATCH 5/9] =?UTF-8?q?make=E6=99=BA=E8=83=BD=E6=8C=87=E6=AD=A3?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0try-catch=E6=8D=95=E8=8E=B7=E5=86=85=E5=AD=98?= =?UTF-8?q?=E5=88=86=E9=85=8D=E5=BC=82=E5=B8=B8=EF=BC=9B=E4=BF=AE=E6=94=B9?= =?UTF-8?q?GN=E6=A0=BC=E5=BC=8F=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I21d7106ecf5343930ad1ef1d27f453289e9e50bc --- interfaces/kits/js/BUILD.gn | 2 ++ .../src/mod_fs/class_file/file_n_exporter.cpp | 9 ++++- .../src/mod_fs/class_stat/stat_n_exporter.cpp | 9 ++++- .../mod_fs/class_stream/stream_n_exporter.cpp | 27 +++++++++++++-- .../class_watcher/watcher_n_exporter.cpp | 9 ++++- .../js/src/mod_fs/properties/copy_file.cpp | 33 ++++++++++++++++--- .../kits/js/src/mod_fs/properties/copydir.cpp | 9 ++++- .../src/mod_fs/properties/create_stream.cpp | 9 ++++- .../src/mod_fs/properties/fdopen_stream.cpp | 9 ++++- .../js/src/mod_fs/properties/listfile.cpp | 10 ++++-- .../kits/js/src/mod_fs/properties/lstat.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/movedir.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/open.cpp | 18 ++++++++-- .../src/mod_fs/properties/prop_n_exporter.cpp | 29 +++++++++++++--- .../js/src/mod_fs/properties/read_text.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/stat.cpp | 18 ++++++++-- .../js/src/mod_fs/properties/truncate.cpp | 9 ++++- .../kits/js/src/mod_fs/properties/watcher.cpp | 17 ++++++++-- 19 files changed, 222 insertions(+), 31 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index e1b4ec79a..a428eeeb3 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -163,6 +163,8 @@ ohos_shared_library("fs") { "ipc:ipc_core", "samgr:samgr_proxy", ] + + use_exceptions = true } ohos_shared_library("hash") { diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 4ab935a24..f80b8f74d 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -195,7 +195,14 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - auto rafEntity = make_unique(); + unique_ptr rafEntity; + try { + rafEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(rafEntity))) { HILOGE("Failed to set file entity"); NError(EIO).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index 0b2baa572..214d06611 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -234,7 +234,14 @@ napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr statEntity = make_unique(); + unique_ptr statEntity; + try { + statEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(statEntity))) { HILOGE("Failed to set stat entity"); NError(EINVAL).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 771ad42af..3f765f420 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -169,7 +169,14 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = make_shared(move(bufGuard)); + shared_ptr arg; + try { + arg = make_shared(move(bufGuard)); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); @@ -230,7 +237,14 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) return nullptr; } - auto arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); + shared_ptr arg; + try { + arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf = buf, len = len, filp, offset = offset]() -> NError { if (offset >= 0) { int ret = fseek(filp, static_cast(offset), SEEK_SET); @@ -319,7 +333,14 @@ napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) return nullptr; } - unique_ptr streamEntity = make_unique(); + unique_ptr streamEntity; + try { + streamEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(streamEntity))) { HILOGE("INNER BUG. Failed to wrap entity for obj stream"); NError(EIO).ThrowErr(env); diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index abc578626..6faaa9d9e 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -37,7 +37,14 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr watcherEntity = make_unique(); + unique_ptr watcherEntity; + try { + watcherEntity = make_unique(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!NClass::SetEntityFor(env, funcArg.GetThisVar(), move(watcherEntity))) { NError(EIO).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 7387ac868..0548aaa79 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -80,7 +80,12 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); } - srcFile.fdg = make_unique(ret, true); + try { + srcFile.fdg = make_unique(ret, true); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } } struct stat statbf; @@ -101,7 +106,12 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); } - destFile.fdg = make_unique(ret, true); + try { + destFile.fdg = make_unique(ret, true); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } } return SendFileCore(srcFile, destFile, statbf); } @@ -128,7 +138,14 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; } @@ -199,7 +216,15 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto cbExec = [para = make_shared(move(src), move(dest))]() -> NError { + shared_ptr para; + try { + para = make_shared(move(src), move(dest)); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } + auto cbExec = [para]() -> NError { if (para->src_.isPath && para->dest_.isPath) { return IsAllPath(para->src_, para->dest_); } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 9675747f1..765490999 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -277,7 +277,14 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = CopyDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 7e6667d9e..81123841e 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -85,7 +85,14 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, argPath = move(argPath), argMode = move(argMode)]() -> NError { arg->fp = { fopen(argPath.c_str(), argMode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index 0e8296274..bcfd5bde5 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -85,7 +85,14 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, fd = fd, mode = mode]() -> NError { arg->fp = { fdopen(fd, mode.c_str()), fclose }; if (!arg->fp) { diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 836cde17c..bb841933d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -351,8 +351,14 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); - + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, optionArgsTmp]() -> NError { g_optionArgs = optionArgsTmp; if (g_optionArgs.recursion) { diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 1391bba8a..30f2751b2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -77,7 +77,14 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) } string path = tmp.get(); - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, path]() -> NError { std::unique_ptr lstat_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 532e78180..7dafca9cc 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -74,7 +74,14 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(tmp.get()), arg]() -> NError { std::unique_ptr mkdtemp_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 146b0a558..0847f28cd 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -318,7 +318,14 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [srcPath = string(src.get()), destPath = string(dest.get()), mode = mode, arg]() -> NError { arg->errNo = MoveDirFunc(srcPath, destPath, mode, arg->errfiles); if (arg->errNo) { diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 684a310f3..bc3554fb1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -100,7 +100,14 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } return NVal(); } - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return NVal(); + } fileEntity->fd_.swap(fdg); if (isUri) { fileEntity->path_ = ""; @@ -266,7 +273,14 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto argv = funcArg[NARG_POS::FIRST]; auto cbExec = [arg, argv, path = string(path.get()), mode = mode, env = env]() -> NError { string pathStr = path; diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index c9aee181c..a3da8820c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -107,7 +107,14 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } - auto result = make_shared(); + shared_ptr result; + try { + result = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(tmp.get()), result]() -> NError { std::unique_ptr access_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -393,8 +400,14 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - + shared_ptr arg; + try { + arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return ReadExec(arg, static_cast(buf), len, fd, offset); }; @@ -465,8 +478,14 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(move(bufGuard)); - + shared_ptr arg; + try { + arg = make_shared(move(bufGuard)); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, buf, len, fd, offset]() -> NError { return WriteExec(arg, static_cast(buf), len, fd, offset); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 0436ae4bd..0a6e57411 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -211,7 +211,14 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(NVal(env, funcArg.GetThisVar())); + shared_ptr arg; + try { + arg = make_shared(NVal(env, funcArg.GetThisVar())); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [path = string(path.get()), arg, offset = offset, hasLen = hasLen, len = len]() -> NError { return ReadTextAsync(path, arg, offset, hasLen, len); }; diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index 56ed01920..ba98585ac 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -39,7 +39,14 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; } HILOGE("Invalid parameter"); @@ -100,7 +107,14 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) return nullptr; } - auto arg = make_shared(); + shared_ptr arg; + try { + arg = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } auto cbExec = [arg, fileInfo = make_shared(move(fileInfo))]() -> NError { std::unique_ptr stat_req = { new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 5fdf2aaa8..ec72bf29e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -38,7 +38,14 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - auto fdg = make_unique(fd, false); + unique_ptr fdg; + try { + fdg = make_unique(fd, false); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return { false, FileInfo { false, {}, {} } }; + } return { true, FileInfo { false, {}, move(fdg) } }; }; diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 45e72c264..ef4eeda4d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -64,7 +64,14 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - shared_ptr watcherPtr = make_shared(); + shared_ptr watcherPtr; + try { + watcherPtr = make_shared(); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } if (!watcherPtr->InitNotify(fd)) { NError(errno).ThrowErr(env); return nullptr; @@ -87,7 +94,13 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) return nullptr; } - watcherEntity->data_ = std::make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + try { + watcherEntity->data_ = make_unique(NVal(env, funcArg[NARG_POS::THIRD])); + } catch (const bad_alloc &) { + HILOGE("Failed to request heap memory."); + NError(ENOMEM).ThrowErr(env); + return nullptr; + } watcherEntity->data_->events = events; watcherEntity->data_->env = env; watcherEntity->data_->filename = string(filename.get()); -- Gitee From 0ffe8359a5559fd7ecc6cc050798efd08a8273e4 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Fri, 12 May 2023 15:28:07 +0800 Subject: [PATCH 6/9] =?UTF-8?q?make=E6=99=BA=E8=83=BD=E6=8C=87=E9=92=88Try?= =?UTF-8?q?-catch=E5=A2=9E=E5=8A=A0=E6=A8=A1=E7=89=88=20Signed-off-by:=20y?= =?UTF-8?q?angjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I07d9aebbf03194499c0e7d0248758caa0b3e7ce5 --- .../src/mod_fs/class_file/file_n_exporter.cpp | 7 ++--- .../src/mod_fs/class_stat/stat_n_exporter.cpp | 8 ++---- .../mod_fs/class_stream/stream_n_exporter.cpp | 21 ++++---------- .../class_watcher/watcher_n_exporter.cpp | 7 ++--- interfaces/kits/js/src/mod_fs/common_func.cpp | 1 - interfaces/kits/js/src/mod_fs/common_func.h | 28 +++++++++++++++++++ .../js/src/mod_fs/properties/copy_file.cpp | 28 ++++++------------- .../kits/js/src/mod_fs/properties/copydir.cpp | 7 ++--- .../src/mod_fs/properties/create_stream.cpp | 7 ++--- .../src/mod_fs/properties/fdopen_stream.cpp | 7 ++--- .../js/src/mod_fs/properties/listfile.cpp | 8 ++---- .../kits/js/src/mod_fs/properties/lstat.cpp | 7 ++--- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 7 ++--- .../kits/js/src/mod_fs/properties/movedir.cpp | 7 ++--- .../kits/js/src/mod_fs/properties/open.cpp | 14 +++------- .../src/mod_fs/properties/prop_n_exporter.cpp | 21 ++++---------- .../js/src/mod_fs/properties/read_text.cpp | 8 ++---- .../kits/js/src/mod_fs/properties/stat.cpp | 15 +++------- .../js/src/mod_fs/properties/truncate.cpp | 7 ++--- .../kits/js/src/mod_fs/properties/watcher.cpp | 15 ++++------ 20 files changed, 88 insertions(+), 142 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index f80b8f74d..487603396 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -195,11 +195,8 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr rafEntity; - try { - rafEntity = make_unique(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr rafEntity = CreateUniquePtr(); + if (rafEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index 214d06611..2b6f18e3b 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -21,6 +21,7 @@ #include #include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -234,11 +235,8 @@ napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr statEntity; - try { - statEntity = make_unique(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr statEntity = CreateUniquePtr(); + if (statEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 3f765f420..5b4473213 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -169,11 +169,8 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(move(bufGuard)); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(move(bufGuard)); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -237,11 +234,8 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(NVal(env, funcArg[NARG_POS::FIRST])); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -333,11 +327,8 @@ napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) return nullptr; } - unique_ptr streamEntity; - try { - streamEntity = make_unique(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr streamEntity = CreateUniquePtr(); + if (streamEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index 6faaa9d9e..fa94e3eae 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -37,11 +37,8 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr watcherEntity; - try { - watcherEntity = make_unique(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr watcherEntity = CreateUniquePtr(); + if (watcherEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index b3c289196..dba4366b3 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -25,7 +25,6 @@ #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" -#include "filemgmt_libhilog.h" #include "filemgmt_libn.h" namespace OHOS { diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 7a169bf9d..2851da8f6 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -17,6 +17,7 @@ #define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H #include "fd_guard.h" +#include "filemgmt_libhilog.h" #include "n_val.h" #include "uv.h" @@ -59,6 +60,33 @@ struct CommonFunc { static void fs_req_cleanup(uv_fs_t* req); static std::string GetModeFromFlags(unsigned int flags); }; + +template +std::shared_ptr CreateSharedPtr(Args&&... args) +{ + std::shared_ptr sPtr = nullptr; + try { + sPtr = std::make_shared(std::forward(args)...); + } catch (const std::bad_alloc&) { + HILOGE("Failed to request heap memory."); + return nullptr; + } + return sPtr; +}; + +template +std::unique_ptr CreateUniquePtr(Args&&... args) +{ + std::unique_ptr uPtr = nullptr; + try { + uPtr = std::make_unique(std::forward(args)...); + } catch (const std::bad_alloc&) { + HILOGE("Failed to request heap memory."); + return nullptr; + } + return uPtr; +} + } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 0548aaa79..635d8b5ab 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -80,10 +80,8 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open srcFile with ret: %{public}d", ret); return NError(errno); } - try { - srcFile.fdg = make_unique(ret, true); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + srcFile.fdg = CreateUniquePtr(ret, true); + if (srcFile.fdg == nullptr) { return NError(ENOMEM); } } @@ -106,10 +104,8 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) HILOGE("Failed to open destFile with ret: %{public}d", ret); return NError(errno); } - try { - destFile.fdg = make_unique(ret, true); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + destFile.fdg = CreateUniquePtr(ret, true); + if (destFile.fdg == nullptr) { return NError(ENOMEM); } } @@ -138,13 +134,10 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { - unique_ptr fdg; - try { - fdg = make_unique(fd, false); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); - return { false, FileInfo { false, {}, {} } }; return { false, FileInfo { false, {}, {} } }; + return { false, FileInfo { false, {}, {} } }; } return { true, FileInfo { false, {}, move(fdg) } }; } @@ -216,11 +209,8 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr para; - try { - para = make_shared(move(src), move(dest)); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr para = CreateSharedPtr(move(src), move(dest)); + if (para == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 765490999..7bbbee60d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -277,11 +277,8 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 81123841e..33514fb74 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -85,11 +85,8 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index bcfd5bde5..679c9e304 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -85,11 +85,8 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index bb841933d..768650b35 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -24,6 +24,7 @@ #include #include +#include "common_func.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -351,11 +352,8 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 30f2751b2..cd2b0f904 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -77,11 +77,8 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) } string path = tmp.get(); - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 7dafca9cc..9df5f20c3 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -74,11 +74,8 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 0847f28cd..d7b363837 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -318,11 +318,8 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index bc3554fb1..95e8e39ec 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -100,11 +100,8 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } return NVal(); } - unique_ptr fdg; - try { - fdg = make_unique(fd, false); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return NVal(); } @@ -273,11 +270,8 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index a3da8820c..add8e513c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -107,11 +107,8 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr result; - try { - result = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr result = CreateSharedPtr(); + if (result == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -400,11 +397,8 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(NVal(env, funcArg[NARG_POS::SECOND])); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::SECOND])); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -478,11 +472,8 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg; - try { - arg = make_shared(move(bufGuard)); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(move(bufGuard)); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 0a6e57411..e93f3d228 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -210,12 +210,8 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - shared_ptr arg; - try { - arg = make_shared(NVal(env, funcArg.GetThisVar())); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(NVal(env, funcArg.GetThisVar())); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index ba98585ac..d33297fc1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -39,11 +39,8 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - unique_ptr fdg; - try { - fdg = make_unique(fd, false); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } @@ -106,12 +103,8 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) if (!succ) { return nullptr; } - - shared_ptr arg; - try { - arg = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr arg = CreateSharedPtr(); + if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index ec72bf29e..88f295421 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -38,11 +38,8 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - unique_ptr fdg; - try { - fdg = make_unique(fd, false); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + unique_ptr fdg = CreateUniquePtr(fd, false); + if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index ef4eeda4d..400c84406 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -20,6 +20,7 @@ #include #include #include +#include "common_func.h" #include "ipc_skeleton.h" #include "filemgmt_libhilog.h" #include "tokenid_kit.h" @@ -64,11 +65,8 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - shared_ptr watcherPtr; - try { - watcherPtr = make_shared(); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + shared_ptr watcherPtr = CreateSharedPtr(); + if (watcherPtr == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -93,11 +91,8 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - - try { - watcherEntity->data_ = make_unique(NVal(env, funcArg[NARG_POS::THIRD])); - } catch (const bad_alloc &) { - HILOGE("Failed to request heap memory."); + watcherEntity->data_ = CreateUniquePtr(NVal(env, funcArg[NARG_POS::THIRD])); + if (watcherEntity->data_ == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; } -- Gitee From aade484fe6e932264c3ae5596653fd436a076401 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Fri, 12 May 2023 17:33:50 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E6=94=B9=E5=8A=A8=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Icaa16e098aaa556992c42027fe62093412244e60 --- .../js/src/mod_file/class_file/file_n_exporter.cpp | 8 ++++++++ .../kits/js/src/mod_fileio/properties/read_text.cpp | 11 +++++++++++ .../kits/js/src/mod_securitylabel/security_label.h | 3 +++ utils/filemgmt_libn/src/n_val.cpp | 4 ++++ 4 files changed, 26 insertions(+) diff --git a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp index 3b195879d..c7db1223a 100644 --- a/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_file/class_file/file_n_exporter.cpp @@ -846,6 +846,10 @@ void ReadTextExec(napi_env env, void *data) int result = stat(path.c_str(), &buf); if (fdg.GetFD() != FAILED && result != FAILED) { auto buffer = std::make_unique(buf.st_size + 1); + if (buffer == nullptr) { + UniError(ENOMEM).ThrowErr(env); + return; + } if (read(fdg.GetFD(), buffer.get(), buf.st_size) != FAILED) { asyncCallbackInfo->result = SUCCESS; asyncCallbackInfo->contents = std::string(buffer.get()); @@ -893,6 +897,10 @@ void ReadArrayBufferExec(napi_env env, void *data) int32_t len = (asyncCallbackInfo->length == COMMON_NUM::ZERO) ? (buf.st_size - begin) : asyncCallbackInfo->length; auto buffer = std::make_unique(len + 1); + if (buffer == nullptr) { + UniError(ENOMEM).ThrowErr(env); + return; + } lseek(fdg.GetFD(), begin, SEEK_CUR); if (read(fdg.GetFD(), buffer.get(), len) != FAILED) { asyncCallbackInfo->result = SUCCESS; diff --git a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp index eeea99ce8..afd1135f5 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/read_text.cpp @@ -109,6 +109,11 @@ napi_value ReadText::Sync(napi_env env, napi_callback_info info) len = !hasLen ? statbf.st_size : len; len = ((len < statbf.st_size) ? len : statbf.st_size); std::unique_ptr readbuf = std::make_unique(len + 1); + if (readbuf == nullptr) { + UniError(EINVAL).ThrowErr(env, "file is too large"); + return nullptr; + } + if (memset_s(readbuf.get(), len + 1, 0, len + 1) != EOK) { UniError(errno).ThrowErr(env, "dfs mem error"); return nullptr; @@ -152,6 +157,9 @@ UniError ReadText::AsyncExec(const std::string &path, std::shared_ptrlen = ((arg->len < statbf.st_size) ? arg->len : statbf.st_size); arg->buf = std::make_unique(arg->len); + if (arg->buf == nullptr) { + return UniError(ENOMEM); + } if (position > 0) { arg->len = pread(sfd.GetFD(), arg->buf.get(), arg->len, position); @@ -187,6 +195,9 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) } auto arg = make_shared(NVal(env, funcArg.GetThisVar())); + if (arg == nullptr) { + return nullptr; + } auto cbExec = [path = string(path.get()), arg, position = position, hasLen = hasLen, len = len](napi_env env) -> UniError { diff --git a/interfaces/kits/js/src/mod_securitylabel/security_label.h b/interfaces/kits/js/src/mod_securitylabel/security_label.h index d50feee89..e789b7261 100644 --- a/interfaces/kits/js/src/mod_securitylabel/security_label.h +++ b/interfaces/kits/js/src/mod_securitylabel/security_label.h @@ -51,6 +51,9 @@ public: return DEFAULT_DATA_LEVEL; } std::unique_ptr xattrValue = std::make_unique((long)xattrValueSize + 1); + if (xattrValue == nullptr) { + return ""; + } xattrValueSize = getxattr(path.c_str(), XATTR_KEY, xattrValue.get(), xattrValueSize); if (xattrValueSize == -1 || errno == ENOTSUP) { diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 7341ae157..5e2944006 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -76,6 +76,10 @@ tuple, size_t> NVal::ToUTF8String(string defaultValue) { if (TypeIs(napi_undefined) || TypeIs(napi_function)) { unique_ptr str = make_unique(defaultValue.size() + 1); + if (str == nullptr) { + HILOGE("Failed to request heap memory"); + return { false, nullptr, 0 }; + } copy(defaultValue.begin(), defaultValue.end(), str.get()); str[defaultValue.size()] = '\0'; return make_tuple(true, move(str), defaultValue.size()); -- Gitee From 8246cfd7e7905a8887afcd6a41b49725e92e2097 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Mon, 15 May 2023 17:44:55 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=91=8A=E8=AD=A6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9;=E5=88=9B=E5=BB=BA=E6=8C=87=E9=92=88?= =?UTF-8?q?=E6=A8=A1=E7=89=88=E7=A7=BB=E5=85=A5utils/common=20Signed-off-b?= =?UTF-8?q?y:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I1512d6983268fa1d98a85c8be2c70a314b630882 --- interfaces/kits/js/BUILD.gn | 1 + .../src/mod_fs/class_file/file_n_exporter.cpp | 3 +- .../src/mod_fs/class_stat/stat_n_exporter.cpp | 4 +- .../mod_fs/class_stream/stream_n_exporter.cpp | 7 ++- .../class_watcher/watcher_n_exporter.cpp | 3 +- interfaces/kits/js/src/mod_fs/common_func.cpp | 1 + interfaces/kits/js/src/mod_fs/common_func.h | 28 ---------- .../js/src/mod_fs/properties/copy_file.cpp | 5 +- .../kits/js/src/mod_fs/properties/copydir.cpp | 5 +- .../src/mod_fs/properties/create_stream.cpp | 3 +- .../src/mod_fs/properties/fdopen_stream.cpp | 1 + .../js/src/mod_fs/properties/listfile.cpp | 4 +- .../kits/js/src/mod_fs/properties/lstat.cpp | 3 +- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 3 +- .../kits/js/src/mod_fs/properties/movedir.cpp | 3 +- .../kits/js/src/mod_fs/properties/open.cpp | 5 +- .../src/mod_fs/properties/prop_n_exporter.cpp | 7 ++- .../js/src/mod_fs/properties/read_text.cpp | 3 +- .../kits/js/src/mod_fs/properties/stat.cpp | 56 +++++++++---------- .../js/src/mod_fs/properties/truncate.cpp | 3 +- .../kits/js/src/mod_fs/properties/watcher.cpp | 34 ++++++----- utils/common/include/file_utils.h | 51 +++++++++++++++++ utils/filemgmt_libn/BUILD.gn | 3 + utils/filemgmt_libn/src/n_val.cpp | 21 +++++-- 24 files changed, 157 insertions(+), 100 deletions(-) create mode 100644 utils/common/include/file_utils.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index a428eeeb3..d3138ae37 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -110,6 +110,7 @@ ohos_shared_library("fs") { "${src_path}/common", "${src_path}/common/file_helper", "${src_path}/mod_fs", + "${utils_path}/common/include", "//third_party/libuv/include", ] diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index 487603396..cbdfbc2c0 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -22,6 +22,7 @@ #include #include +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "filemgmt_libn.h" #include "../common_func.h" @@ -195,7 +196,7 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr rafEntity = CreateUniquePtr(); + auto rafEntity = CreateUniquePtr(); if (rafEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index 2b6f18e3b..ff6ff285b 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -21,7 +21,7 @@ #include #include -#include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -235,7 +235,7 @@ napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr statEntity = CreateUniquePtr(); + auto statEntity = CreateUniquePtr(); if (statEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 5b4473213..27b61e012 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -25,6 +25,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "flush.h" #include "stream_entity.h" @@ -169,7 +170,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) return nullptr; } - shared_ptr arg = CreateSharedPtr(move(bufGuard)); + auto arg = CreateSharedPtr(move(bufGuard)); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; @@ -234,7 +235,7 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) return nullptr; } - shared_ptr arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); + auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; @@ -327,7 +328,7 @@ napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) return nullptr; } - unique_ptr streamEntity = CreateUniquePtr(); + auto streamEntity = CreateUniquePtr(); if (streamEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index fa94e3eae..f8fb3d189 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -21,6 +21,7 @@ #include #include "../common_func.h" +#include "file_utils.h" #include "filemgmt_libn.h" #include "filemgmt_libhilog.h" #include "securec.h" @@ -37,7 +38,7 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) return nullptr; } - unique_ptr watcherEntity = CreateUniquePtr(); + auto watcherEntity = CreateUniquePtr(); if (watcherEntity == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index dba4366b3..b3c289196 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -25,6 +25,7 @@ #include "class_stat/stat_n_exporter.h" #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" +#include "filemgmt_libhilog.h" #include "filemgmt_libn.h" namespace OHOS { diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 2851da8f6..7a169bf9d 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -17,7 +17,6 @@ #define INTERFACES_KITS_JS_SRC_MOD_FS_COMMON_FUNC_H #include "fd_guard.h" -#include "filemgmt_libhilog.h" #include "n_val.h" #include "uv.h" @@ -60,33 +59,6 @@ struct CommonFunc { static void fs_req_cleanup(uv_fs_t* req); static std::string GetModeFromFlags(unsigned int flags); }; - -template -std::shared_ptr CreateSharedPtr(Args&&... args) -{ - std::shared_ptr sPtr = nullptr; - try { - sPtr = std::make_shared(std::forward(args)...); - } catch (const std::bad_alloc&) { - HILOGE("Failed to request heap memory."); - return nullptr; - } - return sPtr; -}; - -template -std::unique_ptr CreateUniquePtr(Args&&... args) -{ - std::unique_ptr uPtr = nullptr; - try { - uPtr = std::make_unique(std::forward(args)...); - } catch (const std::bad_alloc&) { - HILOGE("Failed to request heap memory."); - return nullptr; - } - return uPtr; -} - } // namespace ModuleFileIO } // namespace FileManagement } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 635d8b5ab..133d2c30a 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -23,6 +23,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -134,7 +135,7 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg auto [isFd, fd] = pathOrFdFromJsArg.ToInt32(); if (isFd) { - unique_ptr fdg = CreateUniquePtr(fd, false); + auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; @@ -209,7 +210,7 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr para = CreateSharedPtr(move(src), move(dest)); + auto para = CreateSharedPtr(move(src), move(dest)); if (para == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 7bbbee60d..dac6427ff 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -23,6 +23,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "uv.h" @@ -60,8 +61,8 @@ static tuple, unique_ptr, int> ParseAndCheckJsO return { false, nullptr, nullptr, 0 }; } int mode = 0; - bool resGetThirdArg = false; if (funcArg.GetArgc() >= NARG_CNT::THREE) { + bool resGetThirdArg = false; tie(resGetThirdArg, mode) = NVal(env, funcArg[NARG_POS::THIRD]).ToInt32(mode); if (!resGetThirdArg || (mode < COPYMODE_MIN || mode > COPYMODE_MAX)) { HILOGE("Invalid mode"); @@ -277,7 +278,7 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 33514fb74..7bfee7b2d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -21,6 +21,7 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -85,7 +86,7 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index 679c9e304..e8fedd08d 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -21,6 +21,7 @@ #include "class_stream/stream_entity.h" #include "class_stream/stream_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 768650b35..baf29e23e 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -24,7 +24,7 @@ #include #include -#include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -352,7 +352,7 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index cd2b0f904..69321f27c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -20,6 +20,7 @@ #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -77,7 +78,7 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) } string path = tmp.get(); - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 9df5f20c3..65f190c00 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -16,6 +16,7 @@ #include "mkdtemp.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -74,7 +75,7 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index d7b363837..2c8348e37 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -24,6 +24,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "uv.h" @@ -318,7 +319,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 95e8e39ec..99ad255c6 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -26,6 +26,7 @@ #include "class_file/file_n_exporter.h" #include "common_func.h" #include "datashare_helper.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "ipc_skeleton.h" #include "iservice_registry.h" @@ -100,7 +101,7 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } return NVal(); } - unique_ptr fdg = CreateUniquePtr(fd, false); + auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return NVal(); @@ -270,7 +271,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) HILOGE("Invalid mode"); return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index add8e513c..24b73eaa5 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -31,6 +31,7 @@ #include "create_stream.h" #include "fdatasync.h" #include "fdopen_stream.h" +#include "file_utils.h" #include "filemgmt_libn.h" #include "fsync.h" #include "js_native_api.h" @@ -107,7 +108,7 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr result = CreateSharedPtr(); + auto result = CreateSharedPtr(); if (result == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; @@ -397,7 +398,7 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::SECOND])); + auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::SECOND])); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; @@ -472,7 +473,7 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) return nullptr; } - shared_ptr arg = CreateSharedPtr(move(bufGuard)); + auto arg = CreateSharedPtr(move(bufGuard)); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index e93f3d228..840af965a 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -21,6 +21,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS { @@ -210,7 +211,7 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) NError(EINVAL).ThrowErr(env); return nullptr; } - shared_ptr arg = CreateSharedPtr(NVal(env, funcArg.GetThisVar())); + auto arg = CreateSharedPtr(NVal(env, funcArg.GetThisVar())); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index d33297fc1..d9a9e13e9 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -20,6 +20,7 @@ #include "class_stat/stat_entity.h" #include "class_stat/stat_n_exporter.h" #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -39,7 +40,7 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - unique_ptr fdg = CreateUniquePtr(fd, false); + auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; @@ -51,6 +52,24 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs return { false, FileInfo { false, {}, {} } }; }; +static NError CheckFsStat(const FileInfo &fileInfo, uv_fs_t* req) +{ + if (fileInfo.isPath) { + int ret = uv_fs_stat(nullptr, req, fileInfo.path.get(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat file with path"); + return NError(errno); + } + } else { + int ret = uv_fs_fstat(nullptr, req, fileInfo.fdg->GetFD(), nullptr); + if (ret < 0) { + HILOGE("Failed to stat file with fd"); + return NError(errno); + } + } + return NError(ERRNO_NOERR); +} + napi_value Stat::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -71,20 +90,10 @@ napi_value Stat::Sync(napi_env env, napi_callback_info info) NError(ENOMEM).ThrowErr(env); return nullptr; } - if (fileInfo.isPath) { - int ret = uv_fs_stat(nullptr, stat_req.get(), fileInfo.path.get(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with path"); - NError(errno).ThrowErr(env); - return nullptr; - } - } else { - int ret = uv_fs_fstat(nullptr, stat_req.get(), fileInfo.fdg->GetFD(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with fd"); - NError(errno).ThrowErr(env); - return nullptr; - } + auto err = CheckFsStat(fileInfo, stat_req.get()); + if (err) { + err.ThrowErr(env); + return nullptr; } auto stat = CommonFunc::InstantiateStat(env, stat_req->statbuf).val_; @@ -103,7 +112,7 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) if (!succ) { return nullptr; } - shared_ptr arg = CreateSharedPtr(); + auto arg = CreateSharedPtr(); if (arg == nullptr) { NError(ENOMEM).ThrowErr(env); return nullptr; @@ -115,18 +124,9 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) HILOGE("Failed to request heap memory."); return NError(ENOMEM); } - if (fileInfo->isPath) { - int ret = uv_fs_stat(nullptr, stat_req.get(), fileInfo->path.get(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with path"); - return NError(errno); - } - } else { - int ret = uv_fs_fstat(nullptr, stat_req.get(), fileInfo->fdg->GetFD(), nullptr); - if (ret < 0) { - HILOGE("Failed to stat file with fd"); - return NError(errno); - } + auto err = CheckFsStat(*fileInfo, stat_req.get()); + if (err) { + return err; } arg->stat_ = stat_req->statbuf; return NError(ERRNO_NOERR); diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 88f295421..58ca9d4b8 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -20,6 +20,7 @@ #include #include "common_func.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO { @@ -38,7 +39,7 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs NError(EINVAL).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } - unique_ptr fdg = CreateUniquePtr(fd, false); + auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 400c84406..889b157b1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -20,8 +20,8 @@ #include #include #include -#include "common_func.h" #include "ipc_skeleton.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" #include "tokenid_kit.h" #include "../class_watcher/watcher_entity.h" @@ -39,6 +39,22 @@ namespace { } } +static tuple, napi_value, NError> CreateAndCheckForWatcherEntity(napi_env env, int fd) +{ + auto watcherPtr = CreateSharedPtr(); + if (watcherPtr == nullptr) { + return {nullptr, nullptr, NError(ENOMEM)}; + } + if (!watcherPtr->InitNotify(fd)) { + return {watcherPtr, nullptr, NError(errno)}; + } + napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); + if (!objWatcher) { + return {watcherPtr, nullptr, NError(EINVAL)}; + } + return {watcherPtr, objWatcher, NError(ERRNO_NOERR)}; +} + napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) { if (!IsSystemApp()) { @@ -65,19 +81,9 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } int fd = -1; - shared_ptr watcherPtr = CreateSharedPtr(); - if (watcherPtr == nullptr) { - NError(ENOMEM).ThrowErr(env); - return nullptr; - } - if (!watcherPtr->InitNotify(fd)) { - NError(errno).ThrowErr(env); - return nullptr; - } - - napi_value objWatcher = NClass::InstantiateClass(env, WatcherNExporter::className_, {}); - if (!objWatcher) { - NError(EINVAL).ThrowErr(env); + auto [watcherPtr, objWatcher, err] = CreateAndCheckForWatcherEntity(env, fd); + if (watcherPtr == nullptr || !objWatcher) { + err.ThrowErr(env); return nullptr; } diff --git a/utils/common/include/file_utils.h b/utils/common/include/file_utils.h new file mode 100644 index 000000000..bd7740d06 --- /dev/null +++ b/utils/common/include/file_utils.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FILEMGMT_COMMON_FILE_UTILS_H +#define FILEMGMT_COMMON_FILE_UTILS_H + +#include + +#include "filemgmt_libhilog.h" + +namespace OHOS::FileManagement { +template +std::shared_ptr CreateSharedPtr(Args&&... args) +{ + std::shared_ptr sPtr = nullptr; + try { + sPtr = std::make_shared(std::forward(args)...); + } catch (const std::bad_alloc&) { + HILOGE("Failed to request heap memory."); + return nullptr; + } + return sPtr; +}; + +template +std::unique_ptr CreateUniquePtr(Args&&... args) +{ + std::unique_ptr uPtr = nullptr; + try { + uPtr = std::make_unique(std::forward(args)...); + } catch (const std::bad_alloc&) { + HILOGE("Failed to request heap memory."); + return nullptr; + } + return uPtr; +} + +} // namespace OHOS::FileManagement +#endif // FILEMGMT_COMMON_FILE_UTILS_H diff --git a/utils/filemgmt_libn/BUILD.gn b/utils/filemgmt_libn/BUILD.gn index f7dbe0706..09cff7b55 100644 --- a/utils/filemgmt_libn/BUILD.gn +++ b/utils/filemgmt_libn/BUILD.gn @@ -20,6 +20,7 @@ config("libn_public_config") { include_dirs = [ "include", "include/n_async", + "${utils_path}/common/include", ] } @@ -40,6 +41,8 @@ ohos_shared_library("filemgmt_libn") { "${file_api_path}/utils/filemgmt_libhilog", ] + use_exceptions = true + subsystem_name = "filemanagement" part_name = "file_api" } diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 5e2944006..2a7dd1fcc 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -17,8 +17,9 @@ #include -#include "n_error.h" +#include "file_utils.h" #include "filemgmt_libhilog.h" +#include "n_error.h" namespace OHOS { namespace FileManagement { @@ -67,7 +68,11 @@ tuple, size_t> NVal::ToUTF8String() const } size_t bufLen = strLen + 1; - unique_ptr str = make_unique(bufLen); + auto str = CreateUniquePtr(bufLen); + if (str == nullptr) { + NError(ENOMEM).ThrowErr(env_); + return {false, nullptr, 0}; + } status = napi_get_value_string_utf8(env_, val_, str.get(), bufLen, &strLen); return make_tuple(status == napi_ok, move(str), strLen); } @@ -75,10 +80,10 @@ tuple, size_t> NVal::ToUTF8String() const tuple, size_t> NVal::ToUTF8String(string defaultValue) const { if (TypeIs(napi_undefined) || TypeIs(napi_function)) { - unique_ptr str = make_unique(defaultValue.size() + 1); + auto str = CreateUniquePtr(defaultValue.size() + 1); if (str == nullptr) { - HILOGE("Failed to request heap memory"); - return { false, nullptr, 0 }; + NError(ENOMEM).ThrowErr(env_); + return {false, nullptr, 0}; } copy(defaultValue.begin(), defaultValue.end(), str.get()); str[defaultValue.size()] = '\0'; @@ -96,7 +101,11 @@ tuple, size_t> NVal::ToUTF16String() const return {false, nullptr, 0}; } - auto str = make_unique(++strLen); + auto str = CreateUniquePtr(++strLen); + if (str == nullptr) { + NError(ENOMEM).ThrowErr(env_); + return {false, nullptr, 0}; + } status = napi_get_value_string_utf16(env_, val_, str.get(), strLen, nullptr); if (status != napi_ok) { return {false, nullptr, 0}; -- Gitee From 5846a7d1fa07757aec66268f685544aa763d9df2 Mon Sep 17 00:00:00 2001 From: y30045862 Date: Tue, 23 May 2023 14:19:33 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=91=8A=E8=AD=A6review=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20Signed-off-by:=20yangjingbo10=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Icb7687dcae5d7cdb1d78b020d08608ff3b5d547a --- .../src/mod_fs/class_file/file_n_exporter.cpp | 1 + .../src/mod_fs/class_stat/stat_n_exporter.cpp | 1 + .../mod_fs/class_stream/stream_n_exporter.cpp | 3 ++ .../class_watcher/watcher_n_exporter.cpp | 1 + .../js/src/mod_fs/properties/copy_file.cpp | 4 +++ .../kits/js/src/mod_fs/properties/copydir.cpp | 1 + .../src/mod_fs/properties/create_stream.cpp | 1 + .../src/mod_fs/properties/fdopen_stream.cpp | 1 + .../js/src/mod_fs/properties/listfile.cpp | 1 + .../kits/js/src/mod_fs/properties/lstat.cpp | 34 ++++++++++-------- .../kits/js/src/mod_fs/properties/mkdtemp.cpp | 35 +++++++++++-------- .../kits/js/src/mod_fs/properties/movedir.cpp | 1 + .../kits/js/src/mod_fs/properties/open.cpp | 2 ++ .../src/mod_fs/properties/prop_n_exporter.cpp | 3 ++ .../js/src/mod_fs/properties/read_text.cpp | 1 + .../kits/js/src/mod_fs/properties/stat.cpp | 2 ++ .../js/src/mod_fs/properties/truncate.cpp | 1 + .../kits/js/src/mod_fs/properties/watcher.cpp | 2 ++ utils/common/include/file_utils.h | 2 -- utils/filemgmt_libn/src/n_val.cpp | 15 ++++---- 20 files changed, 72 insertions(+), 40 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp index cbdfbc2c0..7623d84ad 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/file_n_exporter.cpp @@ -198,6 +198,7 @@ napi_value FileNExporter::Constructor(napi_env env, napi_callback_info info) auto rafEntity = CreateUniquePtr(); if (rafEntity == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp index ff6ff285b..30ae48f94 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/stat_n_exporter.cpp @@ -237,6 +237,7 @@ napi_value StatNExporter::Constructor(napi_env env, napi_callback_info info) auto statEntity = CreateUniquePtr(); if (statEntity == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp index 27b61e012..6ecdf56b2 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/stream_n_exporter.cpp @@ -172,6 +172,7 @@ napi_value StreamNExporter::Write(napi_env env, napi_callback_info cbInfo) auto arg = CreateSharedPtr(move(bufGuard)); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -237,6 +238,7 @@ napi_value StreamNExporter::Read(napi_env env, napi_callback_info cbInfo) auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::FIRST])); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -330,6 +332,7 @@ napi_value StreamNExporter::Constructor(napi_env env, napi_callback_info cbInfo) auto streamEntity = CreateUniquePtr(); if (streamEntity == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp index f8fb3d189..a1988cbeb 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/watcher_n_exporter.cpp @@ -40,6 +40,7 @@ napi_value WatcherNExporter::Constructor(napi_env env, napi_callback_info info) auto watcherEntity = CreateUniquePtr(); if (watcherEntity == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp index 133d2c30a..77ecd5ea5 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copy_file.cpp @@ -83,6 +83,7 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) } srcFile.fdg = CreateUniquePtr(ret, true); if (srcFile.fdg == nullptr) { + HILOGE("Failed to request heap memory."); return NError(ENOMEM); } } @@ -107,6 +108,7 @@ static NError OpenFile(FileInfo& srcFile, FileInfo& destFile) } destFile.fdg = CreateUniquePtr(ret, true); if (destFile.fdg == nullptr) { + HILOGE("Failed to request heap memory."); return NError(ENOMEM); } } @@ -137,6 +139,7 @@ static tuple ParseJsOperand(napi_env env, NVal pathOrFdFromJsArg if (isFd) { auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } @@ -212,6 +215,7 @@ napi_value CopyFile::Async(napi_env env, napi_callback_info info) auto para = CreateSharedPtr(move(src), move(dest)); if (para == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp index 2792f8787..16329aff9 100755 --- a/interfaces/kits/js/src/mod_fs/properties/copydir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/copydir.cpp @@ -281,6 +281,7 @@ napi_value CopyDir::Async(napi_env env, napi_callback_info info) auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp index 7bfee7b2d..b2ac7d695 100755 --- a/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/create_stream.cpp @@ -88,6 +88,7 @@ napi_value CreateStream::Async(napi_env env, napi_callback_info info) auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp index e8fedd08d..4029464ed 100755 --- a/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/fdopen_stream.cpp @@ -88,6 +88,7 @@ napi_value FdopenStream::Async(napi_env env, napi_callback_info info) shared_ptr arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp index 649bfb93a..3311f6d97 100755 --- a/interfaces/kits/js/src/mod_fs/properties/listfile.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/listfile.cpp @@ -367,6 +367,7 @@ napi_value ListFile::Async(napi_env env, napi_callback_info info) auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp index 69321f27c..407b59515 100644 --- a/interfaces/kits/js/src/mod_fs/properties/lstat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/lstat.cpp @@ -61,6 +61,24 @@ napi_value Lstat::Sync(napi_env env, napi_callback_info info) return stat; } +static NError LstatExec(shared_ptr arg, string path) +{ + std::unique_ptr lstat_req = { + new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!lstat_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); + return NError(errno); + } else { + arg->stat_ = lstat_req->statbuf; + return NError(ERRNO_NOERR); + } +} + napi_value Lstat::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -80,24 +98,12 @@ napi_value Lstat::Async(napi_env env, napi_callback_info info) string path = tmp.get(); auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } auto cbExec = [arg, path]() -> NError { - std::unique_ptr lstat_req = { - new (std::nothrow) uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!lstat_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); - } - int ret = uv_fs_lstat(nullptr, lstat_req.get(), path.c_str(), nullptr); - if (ret < 0) { - HILOGE("Failed to get stat of file by path: %{public}s, ret: %{public}d", path.c_str(), ret); - return NError(errno); - } else { - arg->stat_ = lstat_req->statbuf; - return NError(ERRNO_NOERR); - } + return LstatExec(arg, path); }; auto cbCompl = [arg](napi_env env, NError err) -> NVal { diff --git a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp index 65f190c00..13c22b6d4 100755 --- a/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/mkdtemp.cpp @@ -59,6 +59,24 @@ napi_value Mkdtemp::Sync(napi_env env, napi_callback_info info) return NVal::CreateUTF8String(env, mkdtemp_req->path).val_; } +static NError MkdTempExec(shared_ptr arg, string path) +{ + std::unique_ptr mkdtemp_req = { + new uv_fs_t, CommonFunc::fs_req_cleanup }; + if (!mkdtemp_req) { + HILOGE("Failed to request heap memory."); + return NError(ENOMEM); + } + int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr); + if (ret < 0) { + HILOGE("Failed to create a temporary directory with path"); + return NError(errno); + } else { + *arg = mkdtemp_req->path; + return NError(ERRNO_NOERR); + } +} + napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -77,26 +95,13 @@ napi_value Mkdtemp::Async(napi_env env, napi_callback_info info) auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } auto cbExec = [path = string(tmp.get()), arg]() -> NError { - std::unique_ptr mkdtemp_req = { - new uv_fs_t, CommonFunc::fs_req_cleanup }; - if (!mkdtemp_req) { - HILOGE("Failed to request heap memory."); - return NError(ENOMEM); - } - int ret = uv_fs_mkdtemp(nullptr, mkdtemp_req.get(), path.c_str(), nullptr); - if (ret < 0) { - HILOGE("Failed to create a temporary directory with path"); - return NError(errno); - } else { - *arg = mkdtemp_req->path; - return NError(ERRNO_NOERR); - } + return MkdTempExec(arg, path); }; - auto cbComplete = [arg](napi_env env, NError err) -> NVal { if (err) { return { env, err.GetNapiErr(env) }; diff --git a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp index 89e65f105..aeb71a8d9 100755 --- a/interfaces/kits/js/src/mod_fs/properties/movedir.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/movedir.cpp @@ -322,6 +322,7 @@ napi_value MoveDir::Async(napi_env env, napi_callback_info info) } auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/open.cpp b/interfaces/kits/js/src/mod_fs/properties/open.cpp index 74b6575df..ea5f82bc1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/open.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/open.cpp @@ -103,6 +103,7 @@ static NVal InstantiateFile(napi_env env, int fd, string pathOrUri, bool isUri) } auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return NVal(); } @@ -271,6 +272,7 @@ napi_value Open::Async(napi_env env, napi_callback_info info) } auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 24b73eaa5..b7573c89e 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -110,6 +110,7 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) auto result = CreateSharedPtr(); if (result == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -400,6 +401,7 @@ napi_value PropNExporter::Read(napi_env env, napi_callback_info info) auto arg = CreateSharedPtr(NVal(env, funcArg[NARG_POS::SECOND])); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } @@ -475,6 +477,7 @@ napi_value PropNExporter::Write(napi_env env, napi_callback_info info) auto arg = CreateSharedPtr(move(bufGuard)); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp index 840af965a..262f52da0 100755 --- a/interfaces/kits/js/src/mod_fs/properties/read_text.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/read_text.cpp @@ -213,6 +213,7 @@ napi_value ReadText::Async(napi_env env, napi_callback_info info) } auto arg = CreateSharedPtr(NVal(env, funcArg.GetThisVar())); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/stat.cpp b/interfaces/kits/js/src/mod_fs/properties/stat.cpp index d9a9e13e9..2d60b89c2 100644 --- a/interfaces/kits/js/src/mod_fs/properties/stat.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/stat.cpp @@ -42,6 +42,7 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs } auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } @@ -114,6 +115,7 @@ napi_value Stat::Async(napi_env env, napi_callback_info info) } auto arg = CreateSharedPtr(); if (arg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp index 58ca9d4b8..695b6a45c 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate.cpp @@ -41,6 +41,7 @@ static tuple ParseJsFile(napi_env env, napi_value pathOrFdFromJs } auto fdg = CreateUniquePtr(fd, false); if (fdg == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return { false, FileInfo { false, {}, {} } }; } diff --git a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp index 889b157b1..abb167ec7 100644 --- a/interfaces/kits/js/src/mod_fs/properties/watcher.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/watcher.cpp @@ -43,6 +43,7 @@ static tuple, napi_value, NError> CreateAndCheckForWatch { auto watcherPtr = CreateSharedPtr(); if (watcherPtr == nullptr) { + HILOGE("Failed to request heap memory."); return {nullptr, nullptr, NError(ENOMEM)}; } if (!watcherPtr->InitNotify(fd)) { @@ -99,6 +100,7 @@ napi_value Watcher::CreateWatcher(napi_env env, napi_callback_info info) } watcherEntity->data_ = CreateUniquePtr(NVal(env, funcArg[NARG_POS::THIRD])); if (watcherEntity->data_ == nullptr) { + HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } diff --git a/utils/common/include/file_utils.h b/utils/common/include/file_utils.h index bd7740d06..c6c4b73db 100644 --- a/utils/common/include/file_utils.h +++ b/utils/common/include/file_utils.h @@ -28,7 +28,6 @@ std::shared_ptr CreateSharedPtr(Args&&... args) try { sPtr = std::make_shared(std::forward(args)...); } catch (const std::bad_alloc&) { - HILOGE("Failed to request heap memory."); return nullptr; } return sPtr; @@ -41,7 +40,6 @@ std::unique_ptr CreateUniquePtr(Args&&... args) try { uPtr = std::make_unique(std::forward(args)...); } catch (const std::bad_alloc&) { - HILOGE("Failed to request heap memory."); return nullptr; } return uPtr; diff --git a/utils/filemgmt_libn/src/n_val.cpp b/utils/filemgmt_libn/src/n_val.cpp index 2a7dd1fcc..6cc0ffb0b 100644 --- a/utils/filemgmt_libn/src/n_val.cpp +++ b/utils/filemgmt_libn/src/n_val.cpp @@ -64,14 +64,13 @@ tuple, size_t> NVal::ToUTF8String() const size_t strLen = 0; napi_status status = napi_get_value_string_utf8(env_, val_, nullptr, -1, &strLen); if (status != napi_ok) { - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } size_t bufLen = strLen + 1; auto str = CreateUniquePtr(bufLen); if (str == nullptr) { - NError(ENOMEM).ThrowErr(env_); - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } status = napi_get_value_string_utf8(env_, val_, str.get(), bufLen, &strLen); return make_tuple(status == napi_ok, move(str), strLen); @@ -82,8 +81,7 @@ tuple, size_t> NVal::ToUTF8String(string defaultValue) if (TypeIs(napi_undefined) || TypeIs(napi_function)) { auto str = CreateUniquePtr(defaultValue.size() + 1); if (str == nullptr) { - NError(ENOMEM).ThrowErr(env_); - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } copy(defaultValue.begin(), defaultValue.end(), str.get()); str[defaultValue.size()] = '\0'; @@ -98,17 +96,16 @@ tuple, size_t> NVal::ToUTF16String() const size_t strLen = 0; napi_status status = napi_get_value_string_utf16(env_, val_, nullptr, -1, &strLen); if (status != napi_ok) { - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } auto str = CreateUniquePtr(++strLen); if (str == nullptr) { - NError(ENOMEM).ThrowErr(env_); - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } status = napi_get_value_string_utf16(env_, val_, str.get(), strLen, nullptr); if (status != napi_ok) { - return {false, nullptr, 0}; + return { false, nullptr, 0 }; } strLen = reinterpret_cast(str.get() + strLen) - reinterpret_cast(str.get()); -- Gitee