Skip to content

Commit e416da5

Browse files
committed
Fix broken HIDPowerDevice_::sendDate method
The method currently uses a local "bval" variable as argument when calling HID().SendReport(...). This is problematic, since the SendReport method doesn't use "bval" immediately. It instead captures a pointer to "bval" which is accessed when the report is sent at a later point. This leads to a use-after-free situation when the pointer captured by SendReport may no longer point to "bval". Propose to fix the issue by introducing a new "iManufacturerDate" member in the HIDPowerDevice class to act as persistent storage for the date value.
1 parent 0801b11 commit e416da5

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/HIDPowerDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ void HIDPowerDevice_::end(void) {
251251
}
252252

253253
int HIDPowerDevice_::sendDate(uint16_t id, uint16_t year, uint8_t month, uint8_t day) {
254-
uint16_t bval = (year - 1980)*512 + month * 32 + day;
255-
return HID().SendReport(id, &bval, sizeof (bval));
254+
iManufacturerDate = (year - 1980)*512 + month * 32 + day; // from 4.2.6 Battery Settings in "Universal Serial Bus Usage Tables for HID Power Devices"
255+
return HID().SendReport(id, &iManufacturerDate, sizeof(iManufacturerDate));
256256
}
257257

258258
int HIDPowerDevice_::sendReport(uint16_t id, const void* bval, int len) {

src/HIDPowerDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class HIDPowerDevice_ {
128128

129129
int setStringFeature(uint8_t id, const uint8_t* index, const char* data);
130130

131+
private:
132+
uint16_t iManufacturerDate = 0;
131133
};
132134

133135
extern HIDPowerDevice_ PowerDevice;

0 commit comments

Comments
 (0)