From 18f90abf0a104f850e06f753326965955f92d48c Mon Sep 17 00:00:00 2001 From: William Dean Date: Tue, 14 Jun 2022 15:44:34 +0800 Subject: [PATCH] Using anonymous namespace to replace global static variable Simplify realpath expression. Signed-off-by: William Dean --- .../remotefileshare_n_exporter.cpp | 29 +++++++----- .../remotefileshare_napi.cpp | 2 +- .../remotefileshare_fuzzer.cpp | 47 ++++++++++--------- 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp index a54e3bf7b..6141fc7ee 100644 --- a/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp +++ b/interfaces/kits/js/remote_file_share/remotefileshare_n_exporter.cpp @@ -13,27 +13,33 @@ * limitations under the License. */ -#include +#include "remotefileshare_n_exporter.h" + #include #include + +#include #include #include #include +#include #include #include #include #include "securec.h" -#include "remotefileshare_n_exporter.h" namespace OHOS { namespace AppFileService { namespace ModuleRemoteFileShare { using namespace FileManagement::LibN; using namespace std; -static constexpr int HMDFS_CID_SIZE = 64; -static constexpr unsigned HMDFS_IOC = 0xf2; +namespace { + constexpr int HMDFS_CID_SIZE = 64; + constexpr unsigned HMDFS_IOC = 0xf2; + const std::string SHARE_PATH = "/data/storage/el2/distributedfiles/.share"; +} #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct hmdfs_share_control) @@ -47,21 +53,19 @@ static NError CreateSharePath(const int src_fd, const std::string &cid) struct hmdfs_share_control sc; int32_t ret = 0; int32_t dirFd; - std::string sharePath = "/data/storage/el2/distributedfiles/.share"; - if (access(sharePath.c_str(), F_OK) != 0) { - ret = mkdir(sharePath.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); + if (access(SHARE_PATH.c_str(), F_OK) != 0) { + ret = mkdir(SHARE_PATH.c_str(), S_IRWXU | S_IRWXG | S_IXOTH); if (ret < 0) { return NError(errno); } } - char *realPath = realpath(sharePath.c_str(), nullptr); - if (realPath == nullptr) { + char realPath[PATH_MAX] = {0}; + if (!realpath(SHARE_PATH.c_str(), realPath)) { return NError(errno); } dirFd = open(realPath, O_RDONLY); - free(realPath); if (dirFd < 0) { return NError(errno); } @@ -113,8 +117,7 @@ napi_value CreateSharePath(napi_env env, napi_callback_info info) if (err) { return { env, err.GetNapiErr(env) }; } else { - std::string sharePath = "/data/storage/el2/distributedfiles/.share"; - return NVal::CreateUTF8String(env, sharePath); + return NVal::CreateUTF8String(env, SHARE_PATH); } }; std::string procedureName = "CreateSharePath"; @@ -134,4 +137,4 @@ napi_value CreateSharePath(napi_env env, napi_callback_info info) } } // namespace ModuleRemoteFileShare } // namespace AppFileService -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp b/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp index fe4329d48..69c672088 100644 --- a/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp +++ b/interfaces/kits/js/remote_file_share/remotefileshare_napi.cpp @@ -34,4 +34,4 @@ napi_value RemoteFileShareExport(napi_env env, napi_value exports) NAPI_MODULE(remotefileshare, RemoteFileShareExport) } // namespace ModuleRemoteFileShare } // namespace AppFileService -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp b/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp index 90f909f0e..560b6123d 100644 --- a/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp +++ b/test/fuzztest/remotefileshare_fuzzer/remotefileshare_fuzzer.cpp @@ -22,13 +22,18 @@ #include #include +#include + #include "securec.h" namespace OHOS { namespace AppFileService { namespace ModuleRemoteFileShare { -static constexpr int HMDFS_CID_SIZE = 64; -static constexpr unsigned HMDFS_IOC = 0xf2; +namespace { + constexpr int HMDFS_CID_SIZE = 64; + constexpr unsigned HMDFS_IOC = 0xf2; + const char* SHARE_PATH = "/data/storage/el2/distributedfiles/.share"; +} #define HMDFS_IOC_SET_SHARE_PATH _IOW(HMDFS_IOC, 1, struct hmdfs_share_control) @@ -42,25 +47,23 @@ bool ShareFilePathIoctlFdAndCidFuzzTest(const uint8_t* data, size_t size) struct hmdfs_share_control sc; int32_t ret = 0; int32_t dirFd; - const char* sharePath = "/data/storage/el2/distributedfiles/.share"; if (size <= 0) { return false; } - if (access(sharePath, F_OK) != 0) { - ret = mkdir(sharePath, S_IRWXU | S_IRWXG | S_IXOTH); + if (access(SHARE_PATH, F_OK) != 0) { + ret = mkdir(SHARE_PATH, S_IRWXU | S_IRWXG | S_IXOTH); if (ret < 0) { return false; } } - char *realPath = realpath(sharePath, nullptr); - if (realPath == nullptr) { + char realPath[PATH_MAX] = {0}; + if (!realpath(SHARE_PATH, realPath)) { return false; } dirFd = open(realPath, O_RDONLY); - free(realPath); if (dirFd < 0) { return false; } @@ -86,47 +89,50 @@ bool ShareFilePathIoctlCidFuzzTest(const uint8_t* data, size_t size) struct hmdfs_share_control sc; int32_t ret = 0; int32_t dirFd; - const char* sharePath = "/data/storage/el2/distributedfiles/.share"; + int32_t srcFd; if (size <= 0) { return false; } - if (access(sharePath, F_OK) != 0) { - ret = mkdir(sharePath, S_IRWXU | S_IRWXG | S_IXOTH); + if (access(SHARE_PATH, F_OK) != 0) { + ret = mkdir(SHARE_PATH, S_IRWXU | S_IRWXG | S_IXOTH); if (ret < 0) { return false; } } - char *realPath = realpath(sharePath, nullptr); - if (realPath == nullptr) { + char realPath[PATH_MAX] = {0}; + if (!realpath(SHARE_PATH, realPath)) { return false; } dirFd = open(realPath, O_RDONLY); - free(realPath); if (dirFd < 0) { return false; } const char* srcPath = "/data/service/el2/100/hmdfs/non_account/data/com.ohos.camera"; - int32_t srcFd = open(srcPath, O_RDONLY); + srcFd = open(srcPath, O_RDONLY); if (srcFd < 0) { + close(dirFd); return false; } sc.src_fd = size; const char* cid = reinterpret_cast(data); if (memcpy_s(sc.cid, HMDFS_CID_SIZE, cid, size) != 0) { close(dirFd); + close(srcFd); return false; } ret = ioctl(dirFd, HMDFS_IOC_SET_SHARE_PATH, &sc); if (ret < 0) { close(dirFd); + close(srcFd); return false; } + close(srcFd); return true; } @@ -135,25 +141,24 @@ bool ShareFilePathIoctlFdFuzzTest(const uint8_t* data, size_t size) struct hmdfs_share_control sc; int32_t ret = 0; int32_t dirFd; - const char* sharePath = "/data/storage/el2/distributedfiles/.share"; if (size <= 0) { return false; } - if (access(sharePath, F_OK) != 0) { - ret = mkdir(sharePath, S_IRWXU | S_IRWXG | S_IXOTH); + if (access(SHARE_PATH, F_OK) != 0) { + ret = mkdir(SHARE_PATH, S_IRWXU | S_IRWXG | S_IXOTH); if (ret < 0) { return false; } } - char *realPath = realpath(sharePath, nullptr); - if (realPath == nullptr) { + char realPath[PATH_MAX] = {0}; + if (!realpath(SHARE_PATH, realPath)) { return false; } + dirFd = open(realPath, O_RDONLY); - free(realPath); if (dirFd < 0) { return false; } -- Gitee