Skip to content

Commit c6eb028

Browse files
committed
Don't set num_hid_devices before it's validated
1 parent 5a81f27 commit c6eb028

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

shared-module/usb_hid/__init__.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ static const uint8_t usb_hid_descriptor_template[] = {
7979
static supervisor_allocation *hid_report_descriptor_allocation;
8080
static usb_hid_device_obj_t hid_devices[MAX_HID_DEVICES];
8181
// If 0, USB HID is disabled.
82-
static mp_int_t hid_devices_num;
82+
static mp_int_t num_hid_devices;
8383

8484
// This tuple is store in usb_hid.devices.
8585
static mp_obj_tuple_t *hid_devices_tuple;
@@ -97,7 +97,7 @@ static mp_obj_tuple_t default_hid_devices_tuple = {
9797
};
9898

9999
bool usb_hid_enabled(void) {
100-
return hid_devices_num > 0;
100+
return num_hid_devices > 0;
101101
}
102102

103103
void usb_hid_set_defaults(void) {
@@ -140,13 +140,13 @@ size_t usb_hid_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *desc
140140
// Make up a fresh tuple containing the device objects saved in the static
141141
// devices table. Save the tuple in usb_hid.devices.
142142
static void usb_hid_set_devices_from_hid_devices(void) {
143-
mp_obj_t tuple_items[hid_devices_num];
144-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
143+
mp_obj_t tuple_items[num_hid_devices];
144+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
145145
tuple_items[i] = &hid_devices[i];
146146
}
147147

148148
// Remember tuple for gc purposes.
149-
hid_devices_tuple = mp_obj_new_tuple(hid_devices_num, tuple_items);
149+
hid_devices_tuple = mp_obj_new_tuple(num_hid_devices, tuple_items);
150150
usb_hid_set_devices(hid_devices_tuple);
151151
}
152152

@@ -165,10 +165,10 @@ bool common_hal_usb_hid_enable(const mp_obj_t devices) {
165165
mp_raise_ValueError_varg(translate("No more than %d HID devices allowed"), MAX_HID_DEVICES);
166166
}
167167

168-
hid_devices_num = num_devices;
168+
num_hid_devices = num_devices;
169169

170170
// Remember the devices in static storage so they live across VMs.
171-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
171+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
172172
// devices has already been validated to contain only usb_hid_device_obj_t objects.
173173
usb_hid_device_obj_t *device =
174174
MP_OBJ_TO_PTR(mp_obj_subscr(devices, MP_OBJ_NEW_SMALL_INT(i), MP_OBJ_SENTINEL));
@@ -185,20 +185,20 @@ void usb_hid_setup_devices(void) {
185185
usb_hid_set_devices_from_hid_devices();
186186

187187
// Create report buffers on the heap.
188-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
188+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
189189
usb_hid_device_create_report_buffers(&hid_devices[i]);
190190
}
191191
}
192192

193193
// Total length of the report descriptor, with all configured devices.
194194
size_t usb_hid_report_descriptor_length(void) {
195195
size_t total_hid_report_descriptor_length = 0;
196-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
196+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
197197
total_hid_report_descriptor_length += hid_devices[i].report_descriptor_length;
198198
}
199199

200200
// Don't need space for a report id if there's only one device.
201-
if (hid_devices_num == 1) {
201+
if (num_hid_devices == 1) {
202202
total_hid_report_descriptor_length -= 2;
203203
}
204204
return total_hid_report_descriptor_length;
@@ -212,10 +212,10 @@ void usb_hid_build_report_descriptor(uint8_t *report_descriptor_space, size_t re
212212

213213
uint8_t *report_descriptor_start = report_descriptor_space;
214214

215-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
215+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
216216
usb_hid_device_obj_t *device = &hid_devices[i];
217217
// Copy the report descriptor for this device.
218-
if (hid_devices_num == 1) {
218+
if (num_hid_devices == 1) {
219219
// There's only one device, so it shouldn't have a report ID.
220220
// Copy the descriptor, but splice out the report id indicator and value (2 bytes).
221221
memcpy(report_descriptor_start, device->report_descriptor, device->report_id_index - 1);
@@ -260,13 +260,13 @@ void usb_hid_gc_collect(void) {
260260
gc_collect_ptr(hid_devices_tuple);
261261

262262
// Mark any heap pointers in the static device list as in use.
263-
for (mp_int_t i = 0; i < hid_devices_num; i++) {
263+
for (mp_int_t i = 0; i < num_hid_devices; i++) {
264264
gc_collect_ptr(&hid_devices[i]);
265265
}
266266
}
267267

268268
usb_hid_device_obj_t *usb_hid_get_device_with_report_id(uint8_t report_id) {
269-
for (uint8_t i = 0; i < hid_devices_num; i++) {
269+
for (uint8_t i = 0; i < num_hid_devices; i++) {
270270
usb_hid_device_obj_t *device = &hid_devices[i];
271271
if (device->report_id == report_id) {
272272
return &hid_devices[i];

0 commit comments

Comments
 (0)