Skip to content

Commit 2f3b21a

Browse files
committed
fix some warnings detected by pvs-studio
1 parent e93e47a commit 2f3b21a

File tree

10 files changed

+42
-79
lines changed

10 files changed

+42
-79
lines changed

examples/device/board_test/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int main(void) {
4545

4646
uint32_t start_ms = 0;
4747
bool led_state = false;
48+
const size_t hello_len = strlen(HELLO_STR);
4849

4950
while (1) {
5051
uint32_t interval_ms = board_button_read() ? BLINK_PRESSED : BLINK_UNPRESSED;
@@ -66,7 +67,7 @@ int main(void) {
6667
printf(HELLO_STR);
6768

6869
#ifndef LOGGER_UART
69-
board_uart_write(HELLO_STR, strlen(HELLO_STR));
70+
board_uart_write(HELLO_STR, hello_len);
7071
#endif
7172
}
7273

examples/device/cdc_msc/src/msc_disk.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uin
126126
const char pid[] = "Mass Storage";
127127
const char rev[] = "1.0";
128128

129-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
130-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
131-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
129+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
130+
strncpy((char*) inquiry_resp->product_id, pid, 16);
131+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
132132

133133
return sizeof(scsi_inquiry_resp_t); // 36 bytes
134134
}
@@ -242,6 +242,8 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u
242242
// negative means error -> tinyusb could stall and/or response with failed status
243243
return -1;
244244
}
245+
246+
return -1;
245247
}
246248

247249
#endif

examples/device/cdc_msc_freertos/src/msc_disk.c

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t* inquiry_resp, uin
198198
const char pid[] = "Mass Storage";
199199
const char rev[] = "1.0";
200200

201-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
202-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
203-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
201+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
202+
strncpy((char*) inquiry_resp->product_id, pid, 16);
203+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
204204

205205
return sizeof(scsi_inquiry_resp_t); // 36 bytes
206206
}
@@ -324,35 +324,19 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
324324
// - READ10 and WRITE10 has their own callbacks
325325
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
326326
// read10 & write10 has their own callback and MUST not be handled here
327-
328-
void const *response = NULL;
329-
int32_t resplen = 0;
330-
331-
// most scsi handled is input
332-
bool in_xfer = true;
327+
(void) buffer;
328+
(void) bufsize;
333329

334330
switch (scsi_cmd[0]) {
335331
default:
336332
// Set Sense = Invalid Command Operation
337333
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
338334

339335
// negative means error -> tinyusb could stall and/or response with failed status
340-
resplen = -1;
341-
break;
342-
}
343-
344-
// return resplen must not larger than bufsize
345-
if (resplen > bufsize) { resplen = bufsize; }
346-
347-
if (response && (resplen > 0)) {
348-
if (in_xfer) {
349-
memcpy(buffer, response, (size_t) resplen);
350-
} else {
351-
// SCSI output
352-
}
336+
return -1;
353337
}
354338

355-
return (int32_t) resplen;
339+
return -1;
356340
}
357341

358342
#endif

examples/device/dynamic_configuration/src/msc_disk.c

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uin
126126
const char pid[] = "Mass Storage";
127127
const char rev[] = "1.0";
128128

129-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
130-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
131-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
129+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
130+
strncpy((char*) inquiry_resp->product_id, pid, 16);
131+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
132132

133133
return sizeof(scsi_inquiry_resp_t); // 36 bytes
134134
}
@@ -211,42 +211,21 @@ int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t*
211211
// Callback invoked when received an SCSI command not in built-in list below
212212
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
213213
// - READ10 and WRITE10 has their own callbacks
214-
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
215-
{
214+
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) {
216215
// read10 & write10 has their own callback and MUST not be handled here
216+
(void) buffer;
217+
(void) bufsize;
217218

218-
void const* response = NULL;
219-
int32_t resplen = 0;
220-
221-
// most scsi handled is input
222-
bool in_xfer = true;
223-
224-
switch (scsi_cmd[0])
225-
{
219+
switch (scsi_cmd[0]) {
226220
default:
227221
// Set Sense = Invalid Command Operation
228222
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
229223

230224
// negative means error -> tinyusb could stall and/or response with failed status
231-
resplen = -1;
232-
break;
233-
}
234-
235-
// return resplen must not larger than bufsize
236-
if ( resplen > bufsize ) resplen = bufsize;
237-
238-
if ( response && (resplen > 0) )
239-
{
240-
if(in_xfer)
241-
{
242-
memcpy(buffer, response, (size_t) resplen);
243-
}else
244-
{
245-
// SCSI output
246-
}
225+
return -1;
247226
}
248227

249-
return resplen;
228+
return -1;
250229
}
251230

252231
#endif

examples/device/msc_dual_lun/src/msc_disk_dual.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,19 +217,17 @@ uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uin
217217
const char pid[] = "Mass Storage";
218218
const char rev[] = "1.0";
219219

220-
memcpy(inquiry_resp->vendor_id, vid, strlen(vid));
221-
memcpy(inquiry_resp->product_id, pid, strlen(pid));
222-
memcpy(inquiry_resp->product_rev, rev, strlen(rev));
220+
strncpy((char*) inquiry_resp->vendor_id, vid, 8);
221+
strncpy((char*) inquiry_resp->product_id, pid, 16);
222+
strncpy((char*) inquiry_resp->product_rev, rev, 4);
223223

224224
return sizeof(scsi_inquiry_resp_t); // 36 bytes
225225
}
226226

227227
// Invoked when received Test Unit Ready command.
228228
// return true allowing host to read/write this LUN e.g SD card inserted
229229
bool tud_msc_test_unit_ready_cb(uint8_t lun) {
230-
if ( lun == 1 && board_button_read() ) return false;
231-
232-
return true; // RAM disk is always ready
230+
return ( lun == 1 && board_button_read() ) ? false : true;
233231
}
234232

235233
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size

hw/bsp/stm32h7/family.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void board_init(void) {
131131

132132
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
133133
// Explicitly disable systick to prevent its ISR runs before scheduler start
134-
SysTick->CTRL &= ~1U;
134+
SysTick->CTRL &= ~1UL;
135135

136136
// If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
137137
#ifdef USB_OTG_FS_PERIPH_BASE

src/class/cdc/cdc_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
533533
// Check for wanted char and invoke callback if needed
534534
if (((signed char) p_cdc->wanted_char) != -1) {
535535
for (uint32_t i = 0; i < xferred_bytes; i++) {
536-
if ((p_cdc->wanted_char == p_epbuf->epout[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
536+
if ((p_cdc->wanted_char == (char) p_epbuf->epout[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
537537
tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
538538
}
539539
}

src/class/cdc/cdc_host.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,6 @@ static inline bool ftdi_sio_reset(cdch_interface_t *p_cdc, tuh_xfer_cb_t complet
11361136

11371137
// internal control complete to update state such as line state, line_coding
11381138
static void ftdi_internal_control_complete(cdch_interface_t* p_cdc, tuh_xfer_t *xfer) {
1139-
TU_VERIFY(xfer->result == XFER_RESULT_SUCCESS,);
11401139
const tusb_control_request_t * setup = xfer->setup;
11411140
if (xfer->result == XFER_RESULT_SUCCESS) {
11421141
if (setup->bRequest == FTDI_SIO_SET_MODEM_CTRL_REQUEST &&

src/common/tusb_types.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ typedef enum {
100100
} tusb_xfer_type_t;
101101

102102
typedef enum {
103-
TUSB_DIR_OUT = 0,
104-
TUSB_DIR_IN = 1,
103+
TUSB_DIR_OUT = 0u,
104+
TUSB_DIR_IN = 1u,
105105

106-
TUSB_DIR_IN_MASK = 0x80
106+
TUSB_DIR_IN_MASK = 0x80u
107107
} tusb_dir_t;
108108

109109
enum {
@@ -350,7 +350,7 @@ typedef struct TU_ATTR_PACKED {
350350
uint8_t bNumConfigurations ; ///< Number of possible configurations.
351351
} tusb_desc_device_t;
352352

353-
TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18, "size is not correct");
353+
TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18u, "size is not correct");
354354

355355
// USB Binary Device Object Store (BOS) Descriptor
356356
typedef struct TU_ATTR_PACKED {
@@ -360,7 +360,7 @@ typedef struct TU_ATTR_PACKED {
360360
uint8_t bNumDeviceCaps ; ///< Number of device capability descriptors in the BOS
361361
} tusb_desc_bos_t;
362362

363-
TU_VERIFY_STATIC( sizeof(tusb_desc_bos_t) == 5, "size is not correct");
363+
TU_VERIFY_STATIC( sizeof(tusb_desc_bos_t) == 5u, "size is not correct");
364364

365365
/// USB Configuration Descriptor
366366
typedef struct TU_ATTR_PACKED {
@@ -375,7 +375,7 @@ typedef struct TU_ATTR_PACKED {
375375
uint8_t bMaxPower ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA).
376376
} tusb_desc_configuration_t;
377377

378-
TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct");
378+
TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9u, "size is not correct");
379379

380380
/// USB Interface Descriptor
381381
typedef struct TU_ATTR_PACKED {
@@ -391,7 +391,7 @@ typedef struct TU_ATTR_PACKED {
391391
uint8_t iInterface ; ///< Index of string descriptor describing this interface
392392
} tusb_desc_interface_t;
393393

394-
TU_VERIFY_STATIC( sizeof(tusb_desc_interface_t) == 9, "size is not correct");
394+
TU_VERIFY_STATIC( sizeof(tusb_desc_interface_t) == 9u, "size is not correct");
395395

396396
/// USB Endpoint Descriptor
397397
typedef struct TU_ATTR_PACKED {
@@ -411,7 +411,7 @@ typedef struct TU_ATTR_PACKED {
411411
uint8_t bInterval ; // Polling interval, in frames or microframes depending on the operating speed
412412
} tusb_desc_endpoint_t;
413413

414-
TU_VERIFY_STATIC( sizeof(tusb_desc_endpoint_t) == 7, "size is not correct");
414+
TU_VERIFY_STATIC( sizeof(tusb_desc_endpoint_t) == 7u, "size is not correct");
415415

416416
/// USB Other Speed Configuration Descriptor
417417
typedef struct TU_ATTR_PACKED {
@@ -441,7 +441,7 @@ typedef struct TU_ATTR_PACKED {
441441
uint8_t bReserved ; ///< Reserved for future use, must be zero
442442
} tusb_desc_device_qualifier_t;
443443

444-
TU_VERIFY_STATIC( sizeof(tusb_desc_device_qualifier_t) == 10, "size is not correct");
444+
TU_VERIFY_STATIC( sizeof(tusb_desc_device_qualifier_t) == 10u, "size is not correct");
445445

446446
/// USB Interface Association Descriptor (IAD ECN)
447447
typedef struct TU_ATTR_PACKED {
@@ -458,7 +458,7 @@ typedef struct TU_ATTR_PACKED {
458458
uint8_t iFunction ; ///< Index of the string descriptor describing the interface association.
459459
} tusb_desc_interface_assoc_t;
460460

461-
TU_VERIFY_STATIC( sizeof(tusb_desc_interface_assoc_t) == 8, "size is not correct");
461+
TU_VERIFY_STATIC( sizeof(tusb_desc_interface_assoc_t) == 8u, "size is not correct");
462462

463463
// USB String Descriptor
464464
typedef struct TU_ATTR_PACKED {
@@ -528,7 +528,7 @@ typedef struct TU_ATTR_PACKED {
528528
uint16_t wLength;
529529
} tusb_control_request_t;
530530

531-
TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct");
531+
TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8u, "size is not correct");
532532

533533
TU_ATTR_PACKED_END // End of all packed definitions
534534
TU_ATTR_BIT_FIELD_ORDER_END

src/device/usbd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
299299
// Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
300300
#define TUD_HID_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epin, _epsize, _ep_interval) \
301301
/* Interface */\
302-
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_HID, (uint8_t)((_boot_protocol) ? (uint8_t)HID_SUBCLASS_BOOT : 0), _boot_protocol, _stridx,\
302+
9, TUSB_DESC_INTERFACE, _itfnum, 0, 1, TUSB_CLASS_HID, (uint8_t)((_boot_protocol != HID_ITF_PROTOCOL_NONE) ? (uint8_t)HID_SUBCLASS_BOOT : 0u), _boot_protocol, _stridx,\
303303
/* HID descriptor */\
304304
9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\
305305
/* Endpoint In */\
@@ -312,7 +312,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
312312
// Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval
313313
#define TUD_HID_INOUT_DESCRIPTOR(_itfnum, _stridx, _boot_protocol, _report_desc_len, _epout, _epin, _epsize, _ep_interval) \
314314
/* Interface */\
315-
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_HID, (uint8_t)((_boot_protocol) ? (uint8_t)HID_SUBCLASS_BOOT : 0), _boot_protocol, _stridx,\
315+
9, TUSB_DESC_INTERFACE, _itfnum, 0, 2, TUSB_CLASS_HID, (uint8_t)((_boot_protocol != HID_ITF_PROTOCOL_NONE) ? (uint8_t)HID_SUBCLASS_BOOT : 0u), _boot_protocol, _stridx,\
316316
/* HID descriptor */\
317317
9, HID_DESC_TYPE_HID, U16_TO_U8S_LE(0x0111), 0, 1, HID_DESC_TYPE_REPORT, U16_TO_U8S_LE(_report_desc_len),\
318318
/* Endpoint Out */\

0 commit comments

Comments
 (0)