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 f323319c16da6ad8ed770b2c438e05914d25b935..5899c9c325b7b31d835e76c9ba9a88ea9f5a4308 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 @@ -66,6 +66,13 @@ napi_value FileNExporter::GetFD(napi_env env, napi_callback_info info) HILOGE("Failed to get file entity"); return nullptr; } + + if (!rafEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); + NError(EIO).ThrowErr(env); + return nullptr; + } + return NVal::CreateInt32(env, rafEntity->fd_.get()->GetFD()).val_; } @@ -264,6 +271,12 @@ napi_value FileNExporter::TryLock(napi_env env, napi_callback_info info) return nullptr; } + if (!fileEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); + NError(EIO).ThrowErr(env); + return nullptr; + } + bool exclusive = false; if (!GetExclusive(env, funcArg, exclusive)) { return nullptr; @@ -296,6 +309,12 @@ napi_value FileNExporter::UnLock(napi_env env, napi_callback_info info) return nullptr; } + if (!fileEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); + NError(EIO).ThrowErr(env); + return nullptr; + } + int ret = 0; ret = flock(fileEntity->fd_.get()->GetFD(), LOCK_UN); if (ret < 0) { diff --git a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp index 8ebe6d597cf8d2cfec702ffc45f34666be622468..90288b1d5e251d87e0b87bd9cf75bdc1854b48a7 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/fs_file.cpp @@ -51,6 +51,12 @@ FsResult FsFile::GetFD() const HILOGE("Failed to get file entity"); return FsResult::Error(EINVAL); } + + if (!fileEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); + return FsResult::Error(EIO); + } + return FsResult::Success(fileEntity->fd_.get()->GetFD()); } @@ -130,10 +136,11 @@ FsResult FsFile::Lock(bool exclusive) const return FsResult::Error(EINVAL); } - if (!fileEntity || !fileEntity->fd_.get()) { - HILOGE("File has been closed in Lock cbExec possibly"); + if (!fileEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); return FsResult::Error(EIO); } + int ret = 0; auto mode = exclusive ? LOCK_EX : LOCK_SH; ret = flock(fileEntity->fd_.get()->GetFD(), mode); @@ -152,6 +159,11 @@ FsResult FsFile::TryLock(bool exclusive) const return FsResult::Error(EINVAL); } + if (!fileEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); + return FsResult::Error(EIO); + } + int ret = 0; auto mode = static_cast(exclusive ? LOCK_EX : LOCK_SH); ret = flock(fileEntity->fd_.get()->GetFD(), mode | LOCK_NB); @@ -170,6 +182,11 @@ FsResult FsFile::UnLock() const return FsResult::Error(EINVAL); } + if (!fileEntity->fd_.get()) { + HILOGE("File descriptor pointer is null"); + return FsResult::Error(EIO); + } + int ret = 0; ret = flock(fileEntity->fd_.get()->GetFD(), LOCK_UN); if (ret < 0) {