8000 Minor paranoid nullptr checks in USB interface (#267) · Robotonics/arduino-pico@fec3ea8 · GitHub
[go: up one dir, main page]

Skip to content

Commit fec3ea8

Browse files
Minor paranoid nullptr checks in USB interface (earlephilhower#267)
Identify some spots that GCC's -fanalyzer noted might need nullptr checks, and add them.
1 parent 580970c commit fec3ea8

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

cores/rp2040/RP2040USB.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static uint8_t *GetDescHIDReport(int *len) {
127127

128128
if (report) {
129129
free(report);
130+
report = nullptr;
130131
}
131132

132133
if (__USBInstallKeyboard && __USBInstallMouse) {
@@ -136,21 +137,27 @@ static uint8_t *GetDescHIDReport(int *len) {
136137
};
137138
report_len = sizeof(desc_hid_report);
138139
report = (uint8_t *)malloc(report_len);
139-
memcpy(report, desc_hid_report, report_len);
140+
if (report) {
141+
memcpy(report, desc_hid_report, report_len);
142+
}
140143
} else if (__USBInstallKeyboard && ! __USBInstallMouse) {
141144
uint8_t desc_hid_report[] = {
142145
TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(1))
143146
};
144147
report_len = sizeof(desc_hid_report);
145148
report = (uint8_t *)malloc(report_len);
146-
memcpy(report, desc_hid_report, report_len);
149+
if (report) {
150+
memcpy(report, desc_hid_report, report_len);
151+
}
147152
} else { // if (!__USBInstallKeyboard && __USBInstallMouse) {
148153
uint8_t desc_hid_report[] = {
149154
TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(1))
150155
};
151156
report_len = sizeof(desc_hid_report);
152157
report = (uint8_t *)malloc(report_len);
153-
memcpy(report, desc_hid_report, report_len);
158+
if (report) {
159+
memcpy(report, desc_hid_report, report_len);
160+
}
154161
}
155162

156163
if (len) {
@@ -205,20 +212,22 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
205212

206213
// Combine to one descriptor
207214
usbd_desc_cfg = (uint8_t *)malloc(usbd_desc_len);
208-
bzero(usbd_desc_cfg, usbd_desc_len);
209-
uint8_t *ptr = usbd_desc_cfg;
210-
memcpy(ptr, tud_cfg_desc, sizeof(tud_cfg_desc));
211-
ptr += sizeof(tud_cfg_desc);
212-
if (__USBInstallSerial) {
213-
memcpy(ptr, cdc_desc, sizeof(cdc_desc));
214-
ptr += sizeof(cdc_desc);
215-
}
216-
if (hasHID) {
217-
memcpy(ptr, hid_desc, sizeof(hid_desc));
218-
ptr += sizeof(hid_desc);
219-
}
220-
if (__USBInstallMIDI) {
221-
memcpy(ptr, midi_desc, sizeof(midi_desc));
215+
if (usbd_desc_cfg) {
216+
bzero(usbd_desc_cfg, usbd_desc_len);
217+
uint8_t *ptr = usbd_desc_cfg;
218+
memcpy(ptr, tud_cfg_desc, sizeof(tud_cfg_desc));
219+
ptr += sizeof(tud_cfg_desc);
220+
if (__USBInstallSerial) {
221+
memcpy(ptr, cdc_desc, sizeof(cdc_desc));
222+
ptr += sizeof(cdc_desc);
223+
}
224+
if (hasHID) {
225+
memcpy(ptr, hid_desc, sizeof(hid_desc));
226+
ptr += sizeof(hid_desc);
227+
}
228+
if (__USBInstallMIDI) {
229+
memcpy(ptr, midi_desc, sizeof(midi_desc));
230+
}
222231
}
223232
}
224233
return usbd_desc_cfg;

0 commit comments

Comments
 (0)
0