From e49fcd945e56f0d88f30a6e5f534dde63c4195e3 Mon Sep 17 00:00:00 2001 From: liyuke Date: Mon, 10 Mar 2025 20:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0truncate=5Fcore=E5=AF=B9fd?= =?UTF-8?q?=E7=9A=84=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liyuke --- .../js/src/mod_fs/properties/truncate_core.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/interfaces/kits/js/src/mod_fs/properties/truncate_core.cpp b/interfaces/kits/js/src/mod_fs/properties/truncate_core.cpp index fb201ab5..1477d328 100644 --- a/interfaces/kits/js/src/mod_fs/properties/truncate_core.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/truncate_core.cpp @@ -25,6 +25,18 @@ namespace OHOS::FileManagement::ModuleFileIO { using namespace std; +static bool ValidFileInfo(FileInfo &fileInfo) +{ + if (!fileInfo.isPath) { + auto fd = fileInfo.fdg->GetFD(); + if (fd < 0) { + HILOGE("Invalid fd"); + return false; + } + } + return true; +} + static int Truncate(FileInfo &fileInfo, int64_t truncateLen) { if (fileInfo.isPath) { @@ -68,6 +80,11 @@ static int Truncate(FileInfo &fileInfo, int64_t truncateLen) FsResult TruncateCore::DoTruncate(FileInfo &fileInfo, const std::optional &len) { + auto succ = ValidFileInfo(fileInfo); + if (!succ) { + return FsResult::Error(EINVAL); + } + int64_t truncateLen = 0; if (len.has_value()) { truncateLen = len.value(); -- Gitee