From 7496109ff69ad26a7a95cab2570e3b6d6b376043 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:23:34 -0400 Subject: [PATCH 1/4] NintendoPowerA: Make constructor explicit While we're at it we can also ensure that all class members have deterministic initial state. --- include/boo/inputdev/NintendoPowerA.hpp | 4 ++-- lib/inputdev/NintendoPowerA.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/boo/inputdev/NintendoPowerA.hpp b/include/boo/inputdev/NintendoPowerA.hpp index 20096a1..a7373a8 100644 --- a/include/boo/inputdev/NintendoPowerA.hpp +++ b/include/boo/inputdev/NintendoPowerA.hpp @@ -34,7 +34,7 @@ struct INintendoPowerACallback { }; class NintendoPowerA final : public TDeviceBase { - NintendoPowerAState m_last; + NintendoPowerAState m_last{}; void deviceDisconnected() override; void initialCycle() override; void transferCycle() override; @@ -42,7 +42,7 @@ class NintendoPowerA final : public TDeviceBase { void receivedHIDReport(const uint8_t* data, size_t length, HIDReportType tp, uint32_t message) override; public: - NintendoPowerA(DeviceToken*); + explicit NintendoPowerA(DeviceToken*); ~NintendoPowerA() override; }; } // namespace boo diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index fbc46dd..7b477cb 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -8,7 +8,7 @@ namespace boo { NintendoPowerA::NintendoPowerA(DeviceToken* token) : TDeviceBase(dev_typeid(NintendoPowerA), token) {} -NintendoPowerA::~NintendoPowerA() {} +NintendoPowerA::~NintendoPowerA() = default; void NintendoPowerA::deviceDisconnected() { std::lock_guard lk(m_callbackLock); From 2e0c7dc973c7b24e1168facb8da0e7e90a94660e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:26:03 -0400 Subject: [PATCH 2/4] NintendoPowerA: Use deduction guides for locks Same behavior, but without the need to explicitly hardcode the mutex type. --- lib/inputdev/NintendoPowerA.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index 7b477cb..9b78d11 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -11,9 +11,10 @@ NintendoPowerA::NintendoPowerA(DeviceToken* token) NintendoPowerA::~NintendoPowerA() = default; void NintendoPowerA::deviceDisconnected() { - std::lock_guard lk(m_callbackLock); - if (m_callback) + std::lock_guard lk{m_callbackLock}; + if (m_callback != nullptr) { m_callback->controllerDisconnected(); + } } void NintendoPowerA::initialCycle() {} @@ -21,14 +22,16 @@ void NintendoPowerA::initialCycle() {} void NintendoPowerA::transferCycle() { uint8_t payload[8]; size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload)); - if (recvSz != 8) + if (recvSz != 8) { return; + } NintendoPowerAState state = *reinterpret_cast(&payload); - std::lock_guard lk(m_callbackLock); - if (state != m_last && m_callback) + std::lock_guard lk{m_callbackLock}; + if (state != m_last && m_callback != nullptr) { m_callback->controllerUpdate(state); + } m_last = state; } From 4b82c6510fa443b994b53dc9e5d8b93a1ab32eb7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:28:10 -0400 Subject: [PATCH 3/4] NintendoPowerA: Use std::array where applicable --- lib/inputdev/NintendoPowerA.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index 9b78d11..2da33d2 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -1,5 +1,6 @@ #include "boo/inputdev/NintendoPowerA.hpp" +#include #include #include "boo/inputdev/DeviceSignature.hpp" @@ -20,9 +21,9 @@ void NintendoPowerA::deviceDisconnected() { void NintendoPowerA::initialCycle() {} void NintendoPowerA::transferCycle() { - uint8_t payload[8]; - size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload)); - if (recvSz != 8) { + std::array payload; + const size_t recvSz = receiveUSBInterruptTransfer(payload.data(), payload.size()); + if (recvSz != payload.size()) { return; } From f445df17018f1e462d30056b3b25e024c5706682 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 6 Sep 2019 07:30:10 -0400 Subject: [PATCH 4/4] NintendoPowerA: Use std::memcpy within transferCycle() Eliminates undefined behavior within the function. --- lib/inputdev/NintendoPowerA.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/inputdev/NintendoPowerA.cpp b/lib/inputdev/NintendoPowerA.cpp index 2da33d2..788cacd 100644 --- a/lib/inputdev/NintendoPowerA.cpp +++ b/lib/inputdev/NintendoPowerA.cpp @@ -27,7 +27,8 @@ void NintendoPowerA::transferCycle() { return; } - NintendoPowerAState state = *reinterpret_cast(&payload); + NintendoPowerAState state; + std::memcpy(&state, payload.data(), sizeof(state)); std::lock_guard lk{m_callbackLock}; if (state != m_last && m_callback != nullptr) { @@ -41,7 +42,7 @@ void NintendoPowerA::finalCycle() {} void NintendoPowerA::receivedHIDReport(const uint8_t* data, size_t length, HIDReportType tp, uint32_t message) {} bool NintendoPowerAState::operator==(const NintendoPowerAState& other) const { - return memcmp(this, &other, sizeof(NintendoPowerAState)) == 0; + return std::memcmp(this, &other, sizeof(NintendoPowerAState)) == 0; } bool NintendoPowerAState::operator!=(const NintendoPowerAState& other) const { return !operator==(other); }