From ca388fed607b216e3c5f5c3d3db7073cb89e5c84 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Tue, 1 Jul 2025 15:37:52 +0800 Subject: [PATCH] Modify the vibrator crash Signed-off-by: wuzhihuitmac Change-Id: I5084b3ccf1f21abfee785f0089ea765ce372d17d --- .../vibrator/native/vibrator_agent_test.cpp | 6 +++--- utils/common/src/file_utils.cpp | 15 +++++++++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/test/unittest/vibrator/native/vibrator_agent_test.cpp b/test/unittest/vibrator/native/vibrator_agent_test.cpp index 2800b5f..d9bac50 100644 --- a/test/unittest/vibrator/native/vibrator_agent_test.cpp +++ b/test/unittest/vibrator/native/vibrator_agent_test.cpp @@ -2841,7 +2841,7 @@ HWTEST_F(VibratorAgentTest, PlayPatternBySessionId_001, TestSize.Level1) MISC_HILOGI("PlayPatternBySessionId_001 in"); VibratorPattern pattern; int32_t ret = PlayPatternBySessionId(0, pattern); - ASSERT_NE(ret, PARAMETER_ERROR); + ASSERT_EQ(ret, PARAMETER_ERROR); } HWTEST_F(VibratorAgentTest, PlayPatternBySessionId_002, TestSize.Level1) @@ -2890,7 +2890,7 @@ HWTEST_F(VibratorAgentTest, PlayPackageBySessionId_001, TestSize.Level1) MISC_HILOGI("PlayPatternBySessionId_001 in"); VibratorPackage package; int32_t ret = PlayPackageBySessionId(0, package); - ASSERT_NE(ret, PARAMETER_ERROR); + ASSERT_EQ(ret, PARAMETER_ERROR); } HWTEST_F(VibratorAgentTest, PlayPackageBySessionId_002, TestSize.Level1) @@ -2930,7 +2930,7 @@ HWTEST_F(VibratorAgentTest, StopVibrateBySessionId_001, TestSize.Level1) { MISC_HILOGI("StopVibrateBySessionId_001 in"); int32_t ret = StopVibrateBySessionId(0); - ASSERT_NE(ret, PARAMETER_ERROR); + ASSERT_EQ(ret, PARAMETER_ERROR); } HWTEST_F(VibratorAgentTest, StopVibrateBySessionId_002, TestSize.Level1) diff --git a/utils/common/src/file_utils.cpp b/utils/common/src/file_utils.cpp index 78419ef..fff2eac 100755 --- a/utils/common/src/file_utils.cpp +++ b/utils/common/src/file_utils.cpp @@ -187,16 +187,23 @@ std::string ReadFd(const RawFileDescriptor &rawFd) int64_t fdSize = GetFileSize(rawFd.fd); if ((rawFd.offset < 0) || (rawFd.offset > fdSize)) { MISC_HILOGE("offset is invalid, offset:%{public}" PRId64, rawFd.offset); - close(rawFd.fd); return {}; } if ((rawFd.length <= 0) || (rawFd.length > fdSize - rawFd.offset)) { MISC_HILOGE("length is invalid, length:%{public}" PRId64, rawFd.length); - close(rawFd.fd); return {}; } - FILE *fp = fdopen(rawFd.fd, "r"); - CHKPS(fp); + int dupFd = dup(rawFd.fd); + if (dupFd < 0) { + MISC_HILOGE("dup fd failed, fd:%{public}d, errno:%{public}d", rawFd.fd, errno); + return {}; + } + FILE *fp = fdopen(dupFd, "r"); + if (fp == nullptr) { + MISC_HILOGE("fdopen failed, fd:%{public}d, errno:%{public}d", dupFd, errno); + close(dupFd); + return {}; + } if (fseek(fp, rawFd.offset, SEEK_SET) != 0) { MISC_HILOGE("fseek failed, errno:%{public}d", errno); if (fclose(fp) != 0) { -- Gitee