From 32338ea7e69474f891364f727e05e3188ceed753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B1=E6=98=AF=E9=93=B8=E5=B8=81=E6=8D=8F?= Date: Thu, 17 Apr 2025 16:19:28 +0800 Subject: [PATCH 1/3] fix ret Signed-off-by:echos2019 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 就是铸币捏 --- .../native/include/sensor_agent_proxy.h | 7 +++++ frameworks/native/src/sensor_agent_proxy.cpp | 28 ++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 9313d814..5dfb6050 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -67,6 +67,13 @@ private: static std::mutex createChannelMutex_; }; +const int32_t CHECK_CODE = 0x00ABCDEF; + +struct SensorInfoCheck { + int32_t checkCode = CHECK_CODE; + SensorInfo *sensorInfos = nullptr; +} + #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton::GetInstance() } // namespace Sensors } // namespace OHOS diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 092c164f..310530b4 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -26,7 +26,7 @@ namespace Sensors { namespace { constexpr uint32_t MAX_SENSOR_LIST_SIZE = 0Xffff; std::mutex sensorInfoMutex_; -SensorInfo *sensorInfos_ = nullptr; +SensorInfoCheck sensorInfoCheck_; std::mutex sensorActiveInfoMutex_; SensorActiveInfo *sensorActiveInfos_ = nullptr; int32_t sensorInfoCount_ = 0; @@ -366,9 +366,10 @@ void SensorAgentProxy::ClearSensorInfos() const free(sensorActiveInfos_); sensorActiveInfos_ = nullptr; } - CHKPV(sensorInfos_); - free(sensorInfos_); - sensorInfos_ = nullptr; + CHKPV(sensorInfoCheck_.sensorInfos); + free(sensorInfoCheck_.sensorInfos); + sensorInfoCheck_.sensorInfos = nullptr; + sensorInfoCheck_.checkCode = CHECK_CODE; } int32_t SensorAgentProxy::ConvertSensorInfos() const @@ -390,15 +391,20 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const } if (sensorInfoCount_ > 0 && sensorInfoCount_ == static_cast(count)) { return SUCCESS; - } else if (sensorInfoCount_ > 0 && sensorInfoCount_ != static_cast(count)) { + } else if (sensorInfoCount_ > 0 && sensorInfoCount_ != static_cast(count) && + sensorInfoCheck_.checkCode == CHECK_CODE) { SEN_HILOGW("sensorInfos_ error, sensorInfoCount_:%{public}d, sensorListCount:%{public}d", sensorInfoCount_, static_cast(count)); ClearSensorInfos(); + } else if (sensorInfoCheck_.checkCode != CHECK_CODE) { + SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode); + ClearSensorInfos(); } - sensorInfos_ = (SensorInfo *)malloc(sizeof(SensorInfo) * count); - CHKPR(sensorInfos_, ERROR); + sensorInfoCheck_.sensorInfos = (SensorInfo *)malloc(sizeof(SensorInfo) * count); + CHKPR(sensorInfoCheck_.sensorInfos, ERROR); + SEN_HILOGI("Sensor count is %{public}zu", count); for (size_t i = 0; i < count; ++i) { - SensorInfo *sensorInfo = sensorInfos_ + i; + SensorInfo *sensorInfo = sensorInfoCheck_.sensorInfos + i; errno_t ret = strcpy_s(sensorInfo->sensorName, NAME_MAX_LEN, sensorList[i].GetSensorName().c_str()); CHKCR(ret == EOK, ERROR); @@ -418,6 +424,8 @@ int32_t SensorAgentProxy::ConvertSensorInfos() const sensorInfo->power = sensorList[i].GetPower(); sensorInfo->minSamplePeriod = sensorList[i].GetMinSamplePeriodNs(); sensorInfo->maxSamplePeriod = sensorList[i].GetMaxSamplePeriodNs(); + SEN_HILOGI("Sensor %{public}zu: sensorId is %{public}d, sensorTypeId is %{public}d", + i, sensorInfo->sensorId, sensorInfo->sensorTypeId); } sensorInfoCount_ = static_cast(count); return SUCCESS; @@ -435,9 +443,9 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) ClearSensorInfos(); return ERROR; } - *sensorInfo = sensorInfos_; + *sensorInfo = sensorInfoCheck_.sensorInfos; *count = sensorInfoCount_; - PrintSensorData::GetInstance().PrintSensorInfo(sensorInfos_, sensorInfoCount_); + PrintSensorData::GetInstance().PrintSensorInfo(sensorInfoCheck_.sensorInfos, sensorInfoCount_); return SUCCESS; } -- Gitee From 51614eea7f5a6e8865687ab4fc0f1464a234f32e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B1=E6=98=AF=E9=93=B8=E5=B8=81=E6=8D=8F?= Date: Thu, 17 Apr 2025 17:58:02 +0800 Subject: [PATCH 2/3] fix ret Signed-off-by:echos2019 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 就是铸币捏 --- frameworks/native/src/sensor_agent_proxy.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index 310530b4..dfb11bc2 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -370,6 +370,7 @@ void SensorAgentProxy::ClearSensorInfos() const free(sensorInfoCheck_.sensorInfos); sensorInfoCheck_.sensorInfos = nullptr; sensorInfoCheck_.checkCode = CHECK_CODE; + sensorInfoCount_ = 0; } int32_t SensorAgentProxy::ConvertSensorInfos() const @@ -443,6 +444,12 @@ int32_t SensorAgentProxy::GetAllSensors(SensorInfo **sensorInfo, int32_t *count) ClearSensorInfos(); return ERROR; } + if (sensorInfoCheck_.checkCode != CHECK_CODE) { + SEN_HILOGE("CheckCode has been modified, %{public}d", sensorInfoCheck_.checkCode); + ClearSensorInfos(); + return ERROR; + } + CHKPR(sensorInfoCheck_.sensorInfos, OHOS::Sensors::ERROR); *sensorInfo = sensorInfoCheck_.sensorInfos; *count = sensorInfoCount_; PrintSensorData::GetInstance().PrintSensorInfo(sensorInfoCheck_.sensorInfos, sensorInfoCount_); -- Gitee From 4fbc5a0af2c47b34c7587675eb20b66afefdbe78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B1=E6=98=AF=E9=93=B8=E5=B8=81=E6=8D=8F?= Date: Thu, 17 Apr 2025 10:54:19 +0000 Subject: [PATCH 3/3] update frameworks/native/include/sensor_agent_proxy.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 就是铸币捏 --- frameworks/native/include/sensor_agent_proxy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index 5dfb6050..7c2353c0 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -72,7 +72,7 @@ const int32_t CHECK_CODE = 0x00ABCDEF; struct SensorInfoCheck { int32_t checkCode = CHECK_CODE; SensorInfo *sensorInfos = nullptr; -} +}; #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton::GetInstance() } // namespace Sensors -- Gitee