From 75f118878a586345c716b258138ccafa6453e5e6 Mon Sep 17 00:00:00 2001 From: dinghong Date: Mon, 21 Jul 2025 20:34:19 +0800 Subject: [PATCH] fix file dup Signed-off-by: dinghong --- interfaces/kits/cj/src/file_impl.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/cj/src/file_impl.cpp b/interfaces/kits/cj/src/file_impl.cpp index a72bffb60..48728d3f2 100644 --- a/interfaces/kits/cj/src/file_impl.cpp +++ b/interfaces/kits/cj/src/file_impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-2025 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 @@ -253,22 +253,26 @@ std::tuple> FileEntity::Dup(int32_t fd) new (std::nothrow) uv_fs_t, CommonFunc::FsReqCleanup }; if (!readlink_req) { LOGE("Failed to request heap memory."); + close(dstFd); return {ENOMEM, nullptr}; } string path = "/proc/self/fd/" + to_string(dstFd); int ret = uv_fs_readlink(nullptr, readlink_req.get(), path.c_str(), nullptr); if (ret < 0) { LOGE("Failed to readlink fd, ret: %{public}d", ret); + close(dstFd); return {ret, nullptr}; } auto fdPrt = CreateUniquePtr(dstFd, false); if (fdPrt == nullptr) { LOGE("Failed to request heap memory."); + close(dstFd); return {ENOMEM, nullptr}; } auto pathStr = string(static_cast(readlink_req->ptr)); auto fileEntity = FFIData::Create(std::move(fdPrt), pathStr, ""); if (!fileEntity) { + close(dstFd); return {ENOMEM, nullptr}; } return {SUCCESS_CODE, fileEntity}; -- Gitee