From 395e4a980c843e034ca19ef5e041da2ce4bcb591 Mon Sep 17 00:00:00 2001 From: Fredrik Orderud Date: Mon, 9 Sep 2024 12:44:35 +0200 Subject: [PATCH 1/4] HID: Zero-initialize pointer members I'm new to Arduino development, so I'm not sure if variable are zero-initialized by default. Regardless, it's often regarded good practice to explicitly zero-initialize pointers to reduce risk of treating uninitialized pointers as valid. --- src/HID/HID.cpp | 2 +- src/HID/HID.h | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/HID/HID.cpp b/src/HID/HID.cpp index 758f899..3550a4d 100644 --- a/src/HID/HID.cpp +++ b/src/HID/HID.cpp @@ -266,7 +266,7 @@ bool HID_::setup(USBSetup& setup) } HID_::HID_(void) : PluggableUSBModule(2, 1, epType), - rootNode(NULL), descriptorSize(0), + descriptorSize(0), protocol(HID_REPORT_PROTOCOL), idle(1) { epType[0] = EP_TYPE_INTERRUPT_IN; diff --git a/src/HID/HID.h b/src/HID/HID.h index ea1598c..b585258 100644 --- a/src/HID/HID.h +++ b/src/HID/HID.h @@ -141,20 +141,19 @@ class HID_ : public PluggableUSBModule private: uint8_t epType[2]; - HIDSubDescriptor* rootNode; + HIDSubDescriptor* rootNode = nullptr; uint16_t descriptorSize; uint8_t protocol; uint8_t idle; // Buffer pointer to hold the feature data - HIDReport* rootReport; + HIDReport* rootReport = nullptr; uint16_t reportCount; - Serial_ *dbg; - - const char *serial; + Serial_ *dbg = nullptr; + const char *serial = nullptr; }; // Replacement for global singleton. From 2c31d93f8b0fe76e4add29a547817e5a9933516f Mon Sep 17 00:00:00 2001 From: Fredrik Orderud Date: Mon, 9 Sep 2024 12:51:37 +0200 Subject: [PATCH 2/4] Move descriptorSize initialization from cpp to header. --- src/HID/HID.cpp | 1 - src/HID/HID.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/HID/HID.cpp b/src/HID/HID.cpp index 3550a4d..2cb8bf5 100644 --- a/src/HID/HID.cpp +++ b/src/HID/HID.cpp @@ -266,7 +266,6 @@ bool HID_::setup(USBSetup& setup) } HID_::HID_(void) : PluggableUSBModule(2, 1, epType), - descriptorSize(0), protocol(HID_REPORT_PROTOCOL), idle(1) { epType[0] = EP_TYPE_INTERRUPT_IN; diff --git a/src/HID/HID.h b/src/HID/HID.h index b585258..56b5776 100644 --- a/src/HID/HID.h +++ b/src/HID/HID.h @@ -142,7 +142,7 @@ class HID_ : public PluggableUSBModule uint8_t epType[2]; HIDSubDescriptor* rootNode = nullptr; - uint16_t descriptorSize; + uint16_t descriptorSize = 0; uint8_t protocol; uint8_t idle; From e1aa86735622e7691613856803a3c93783e026cd Mon Sep 17 00:00:00 2001 From: Fredrik Orderud Date: Mon, 9 Sep 2024 12:52:44 +0200 Subject: [PATCH 3/4] Move idle member initialization from cpp to header. --- src/HID/HID.cpp | 2 +- src/HID/HID.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HID/HID.cpp b/src/HID/HID.cpp index 2cb8bf5..82d1a15 100644 --- a/src/HID/HID.cpp +++ b/src/HID/HID.cpp @@ -266,7 +266,7 @@ bool HID_::setup(USBSetup& setup) } HID_::HID_(void) : PluggableUSBModule(2, 1, epType), - protocol(HID_REPORT_PROTOCOL), idle(1) + protocol(HID_REPORT_PROTOCOL) { epType[0] = EP_TYPE_INTERRUPT_IN; epType[1] = EP_TYPE_INTERRUPT_OUT; diff --git a/src/HID/HID.h b/src/HID/HID.h index 56b5776..abf47b0 100644 --- a/src/HID/HID.h +++ b/src/HID/HID.h @@ -145,7 +145,7 @@ class HID_ : public PluggableUSBModule uint16_t descriptorSize = 0; uint8_t protocol; - uint8_t idle; + uint8_t idle = 1; // Buffer pointer to hold the feature data HIDReport* rootReport = nullptr; From 15a16f25e9ae60595b729d5ab99a55c2e8c1a247 Mon Sep 17 00:00:00 2001 From: Fredrik Orderud Date: Mon, 9 Sep 2024 12:53:50 +0200 Subject: [PATCH 4/4] Move protocol member initialization from cpp to header. --- src/HID/HID.cpp | 3 +-- src/HID/HID.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/HID/HID.cpp b/src/HID/HID.cpp index 82d1a15..a4b5fe4 100644 --- a/src/HID/HID.cpp +++ b/src/HID/HID.cpp @@ -265,8 +265,7 @@ bool HID_::setup(USBSetup& setup) return false; } -HID_::HID_(void) : PluggableUSBModule(2, 1, epType), - protocol(HID_REPORT_PROTOCOL) +HID_::HID_(void) : PluggableUSBModule(2, 1, epType) { epType[0] = EP_TYPE_INTERRUPT_IN; epType[1] = EP_TYPE_INTERRUPT_OUT; diff --git a/src/HID/HID.h b/src/HID/HID.h index abf47b0..1da6786 100644 --- a/src/HID/HID.h +++ b/src/HID/HID.h @@ -144,7 +144,7 @@ class HID_ : public PluggableUSBModule HIDSubDescriptor* rootNode = nullptr; uint16_t descriptorSize = 0; - uint8_t protocol; + uint8_t protocol = HID_REPORT_PROTOCOL; uint8_t idle = 1; // Buffer pointer to hold the feature data