From 1d29f93863959713e0351576c4787daf4ef4bbd9 Mon Sep 17 00:00:00 2001 From: leonardian Date: Mon, 8 Jul 2024 17:00:50 +0800 Subject: [PATCH] vibrator: common: Check whether file is opened correctly - Add compile-time type checking inside the has() function to ensure it receives the correct type. - Add is_open() inside the has() function. Bug: 350911314 Test: alarm, ringtone vibrations Test: keyboard vibrations Test: idlcli commands Test: atest PTS, CTS, VTS Flag: EXEMPT bugfix Change-Id: Ifa8f197f6f4b25d51b2edaf6fec27a086bd4f73c --- vibrator/common/HardwareBase.cpp | 4 ---- vibrator/common/HardwareBase.h | 14 +++++++++++++- vibrator/cs40l26/Hardware.h | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/vibrator/common/HardwareBase.cpp b/vibrator/common/HardwareBase.cpp index 7d61b57..329293a 100644 --- a/vibrator/common/HardwareBase.cpp +++ b/vibrator/common/HardwareBase.cpp @@ -40,10 +40,6 @@ void HwApiBase::saveName(const std::string &name, const std::ios *stream) { mNames[stream] = name; } -bool HwApiBase::has(const std::ios &stream) { - return !!stream; -} - void HwApiBase::debug(int fd) { dprintf(fd, "Kernel:\n"); diff --git a/vibrator/common/HardwareBase.h b/vibrator/common/HardwareBase.h index 448d29c..a957848 100644 --- a/vibrator/common/HardwareBase.h +++ b/vibrator/common/HardwareBase.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "utils.h" @@ -67,7 +68,8 @@ class HwApiBase { void saveName(const std::string &name, const std::ios *stream); template void open(const std::string &name, T *stream); - bool has(const std::ios &stream); + template + bool has(const T &stream); template bool get(T *value, std::istream *stream); template @@ -93,6 +95,16 @@ void HwApiBase::open(const std::string &name, T *stream) { utils::openNoCreate(mPathPrefix + name, stream); } +template +bool HwApiBase::has(const T &stream) { + if constexpr (std::is_same::value || std::is_same::value || + std::is_same::value) + return stream.is_open() && !stream.fail(); + + ALOGE("File stream is not of the correct type"); + return false; +} + template bool HwApiBase::get(T *value, std::istream *stream) { ATRACE_NAME("HwApi::get"); diff --git a/vibrator/cs40l26/Hardware.h b/vibrator/cs40l26/Hardware.h index d06fc6d..e2c2d36 100644 --- a/vibrator/cs40l26/Hardware.h +++ b/vibrator/cs40l26/Hardware.h @@ -92,7 +92,7 @@ class HwApi : public Vibrator::HwApi, private HwApiBase { bool setRedc(std::string value) override { return set(value, &mRedc); } bool setQ(std::string value) override { return set(value, &mQ); } bool getEffectCount(uint32_t *value) override { return get(value, &mEffectCount); } - bool hasEffectBrakingTimeBank() override { return mEffectBrakingTimeBank.is_open(); } + bool hasEffectBrakingTimeBank() override { return has(mEffectBrakingTimeBank); } bool setEffectBrakingTimeBank(uint32_t value) override { return set(value, &mEffectBrakingTimeBank); }