diff --git a/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp index 43bae0a641fe559f119748a490977681cc3a716a..0b9df546266b4609f93d6f3c2a240f3ce1143eb2 100644 --- a/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp @@ -168,6 +168,9 @@ napi_value DirNExporter::Read(napi_env env, napi_callback_info info) auto arg = make_shared(NVal(env, funcArg.GetThisVar())); auto cbExec = [arg, dir, dirEntity](napi_env env) -> UniError { struct dirent tmpDirent; + if (memset_s(&tmpDirent, sizeof(struct dirent), 0, sizeof(struct dirent)) != EOK) { + return UniError(errno); + } lock_guard(dirEntity->lock_); errno = 0; dirent *res = nullptr; @@ -216,26 +219,29 @@ napi_value DirNExporter::ReadSync(napi_env env, napi_callback_info info) } struct dirent tmpDirent; - { - lock_guard(dirEntity->lock_); - errno = 0; - dirent *res = nullptr; - do { - res = readdir(dirEntity->dir_.get()); - if (res == nullptr && errno) { - UniError(errno).ThrowErr(env); - return nullptr; - } else if (res == nullptr) { - return NVal::CreateUndefined(env).val_; - } else if (string(res->d_name) == "." || string(res->d_name) == "..") { - continue; - } else { - tmpDirent = *res; - break; - } - } while (true); + if (memset_s(&tmpDirent, sizeof(struct dirent), 0, sizeof(struct dirent)) != EOK) { + UniError(EINVAL).ThrowErr(env, "Get dirent memset fail"); + return nullptr; } + lock_guard(dirEntity->lock_); + errno = 0; + struct dirent *res = nullptr; + do { + res = readdir(dirEntity->dir_.get()); + if (res == nullptr && errno) { + UniError(errno).ThrowErr(env); + return nullptr; + } else if (res == nullptr) { + return NVal::CreateUndefined(env).val_; + } else if (string(res->d_name) == "." || string(res->d_name) == "..") { + continue; + } else { + tmpDirent = *res; + break; + } + } while (true); + napi_value objDirent = NClass::InstantiateClass(env, DirentNExporter::className_, {}); if (!objDirent) { return nullptr;