Skip to content

Commit 65ac519

Browse files
authored
Merge pull request hathach#1852 from silvergasp/mem_s
fix: Replace device calls to memcpy with tu_memcpy_s
2 parents 73afca1 + e34aeb5 commit 65ac519

File tree

9 files changed

+44
-26
lines changed

9 files changed

+44
-26
lines changed

src/class/audio/audio_device.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -823,10 +823,7 @@ uint16_t tud_audio_int_ctr_n_write(uint8_t func_id, uint8_t const* buffer, uint1
823823
// We write directly into the EP's buffer - abort if previous transfer not complete
824824
TU_VERIFY(!usbd_edpt_busy(_audiod_fct[func_id].rhport, _audiod_fct[func_id].ep_int_ctr));
825825

826-
// Check length
827-
TU_VERIFY(len <= CFG_TUD_AUDIO_INT_CTR_EP_IN_SW_BUFFER_SIZE);
828-
829-
memcpy(_audiod_fct[func_id].ep_int_ctr_buf, buffer, len);
826+
TU_VERIFY(tu_memcpy_s(_audiod_fct[func_id].ep_int_ctr_buf, CFG_TUD_AUDIO_INT_CTR_EP_IN_SW_BUFFER_SIZE, buffer, len)==0);
830827

831828
// Schedule transmit
832829
TU_VERIFY(usbd_edpt_xfer(_audiod_fct[func_id].rhport, _audiod_fct[func_id].ep_int_ctr, _audiod_fct[func_id].ep_int_ctr_buf, len));
@@ -2202,7 +2199,7 @@ bool tud_audio_buffer_and_schedule_control_xfer(uint8_t rhport, tusb_control_req
22022199
if (len > _audiod_fct[func_id].ctrl_buf_sz) len = _audiod_fct[func_id].ctrl_buf_sz;
22032200

22042201
// Copy into buffer
2205-
memcpy((void *)_audiod_fct[func_id].ctrl_buf, data, (size_t)len);
2202+
TU_VERIFY(0 == tu_memcpy_s(_audiod_fct[func_id].ctrl_buf, sizeof(_audiod_fct[func_id].ctrl_buf), data, (size_t)len));
22062203

22072204
// Schedule transmit
22082205
return tud_control_xfer(rhport, p_request, (void*)_audiod_fct[func_id].ctrl_buf, len);

src/class/dfu/dfu_rt_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ bool dfu_rtd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
110110
TU_LOG2(" DFU RT Request: GETSTATUS\r\n");
111111
dfu_status_response_t resp;
112112
// Status = OK, Poll timeout is ignored during RT, State = APP_IDLE, IString = 0
113-
memset(&resp, 0x00, sizeof(dfu_status_response_t));
113+
TU_VERIFY(tu_memset_s(&resp, sizeof(resp), 0x00, sizeof(resp))==0);
114114
tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t));
115115
}
116116
break;

src/class/hid/hid_device.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,12 @@ bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, u
9292
// prepare data
9393
if (report_id)
9494
{
95-
len = tu_min16(len, CFG_TUD_HID_EP_BUFSIZE-1);
96-
9795
p_hid->epin_buf[0] = report_id;
98-
memcpy(p_hid->epin_buf+1, report, len);
96+
TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf+1, CFG_TUD_HID_EP_BUFSIZE-1, report, len));
9997
len++;
10098
}else
10199
{
102-
// If report id = 0, skip ID field
103-
len = tu_min16(len, CFG_TUD_HID_EP_BUFSIZE);
104-
memcpy(p_hid->epin_buf, report, len);
100+
TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf, CFG_TUD_HID_EP_BUFSIZE, report, len));
105101
}
106102

107103
return usbd_edpt_xfer(rhport, p_hid->ep_in, p_hid->epin_buf, len);
@@ -126,7 +122,7 @@ bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modi
126122

127123
if ( keycode )
128124
{
129-
memcpy(report.keycode, keycode, 6);
125+
memcpy(report.keycode, keycode, sizeof(report.keycode));
130126
}else
131127
{
132128
tu_memclr(report.keycode, 6);
@@ -151,8 +147,7 @@ bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id,
151147
}
152148

153149
bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id,
154-
int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons)
155-
{
150+
int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) {
156151
hid_gamepad_report_t report =
157152
{
158153
.x = x,
@@ -183,11 +178,12 @@ void hidd_reset(uint8_t rhport)
183178
}
184179

185180
uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len)
186-
{
181+
{
187182
TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass, 0);
188183

189184
// len = interface + hid + n*endpoints
190-
uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) +
185+
uint16_t const drv_len =
186+
(uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) +
191187
desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t));
192188
TU_ASSERT(max_len >= drv_len, 0);
193189

src/class/midi/midi_device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ uint32_t tud_midi_n_stream_read(uint8_t itf, uint8_t cable_num, void* buffer, ui
182182
uint8_t const count = (uint8_t) tu_min32(stream->total - stream->index, bufsize);
183183

184184
// Skip the header (1st byte) in the buffer
185-
memcpy(buf8, stream->buffer + 1 + stream->index, count);
185+
TU_VERIFY(0 == tu_memcpy_s(buf8, bufsize, stream->buffer + 1 + stream->index, count));
186186

187187
total_read += count;
188188
stream->index += count;

src/class/msc/msc_device.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
707707
read_capa10.block_size = tu_htonl(block_size);
708708

709709
resplen = sizeof(read_capa10);
710-
memcpy(buffer, &read_capa10, (size_t) resplen);
710+
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &read_capa10, (size_t) resplen));
711711
}
712712
}
713713
break;
@@ -741,7 +741,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
741741
read_fmt_capa.block_size_u16 = tu_htons(block_size);
742742

743743
resplen = sizeof(read_fmt_capa);
744-
memcpy(buffer, &read_fmt_capa, (size_t) resplen);
744+
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &read_fmt_capa, (size_t) resplen));
745745
}
746746
}
747747
break;
@@ -764,7 +764,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
764764
tud_msc_inquiry_cb(lun, inquiry_rsp.vendor_id, inquiry_rsp.product_id, inquiry_rsp.product_rev);
765765

766766
resplen = sizeof(inquiry_rsp);
767-
memcpy(buffer, &inquiry_rsp, (size_t) resplen);
767+
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &inquiry_rsp, (size_t) resplen));
768768
}
769769
break;
770770

@@ -788,7 +788,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
788788
mode_resp.write_protected = !writable;
789789

790790
resplen = sizeof(mode_resp);
791-
memcpy(buffer, &mode_resp, (size_t) resplen);
791+
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &mode_resp, (size_t) resplen));
792792
}
793793
break;
794794

@@ -806,7 +806,7 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_
806806
sense_rsp.add_sense_qualifier = p_msc->add_sense_qualifier;
807807

808808
resplen = sizeof(sense_rsp);
809-
memcpy(buffer, &sense_rsp, (size_t) resplen);
809+
TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &sense_rsp, (size_t) resplen));
810810

811811
// request sense callback could overwrite the sense data
812812
if (tud_msc_request_sense_cb)

src/common/tusb_common.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ TU_ATTR_WEAK extern void* tusb_app_phys_to_virt(void *phys_addr);
9898
#define tu_memclr(buffer, size) memset((buffer), 0, (size))
9999
#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var)))
100100

101+
// This is a backport of memset_s from c11
102+
TU_ATTR_ALWAYS_INLINE static inline int tu_memset_s(void *dest, size_t destsz, int ch, size_t count)
103+
{
104+
// TODO may check if desst and src is not NULL
105+
if (count > destsz) {
106+
return -1;
107+
}
108+
memset(dest, ch, count);
109+
return 0;
110+
}
111+
112+
// This is a backport of memcpy_s from c11
113+
TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, const void * src, size_t count )
114+
{
115+
// TODO may check if desst and src is not NULL
116+
if (count > destsz) {
117+
return -1;
118+
}
119+
memcpy(dest, src, count);
120+
return 0;
121+
}
122+
123+
101124
//------------- Bytes -------------//
102125
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
103126
{

src/device/dcd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_reset (uint8_t rhport, t
193193
TU_ATTR_ALWAYS_INLINE static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr)
194194
{
195195
dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED };
196-
memcpy(&event.setup_received, setup, 8);
196+
memcpy(&event.setup_received, setup, sizeof(tusb_control_request_t));
197197

198198
dcd_event_handler(&event, in_isr);
199199
}

src/device/usbd_control.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ static bool _data_stage_xact(uint8_t rhport)
9393
if ( _ctrl_xfer.request.bmRequestType_bit.direction == TUSB_DIR_IN )
9494
{
9595
ep_addr = EDPT_CTRL_IN;
96-
if ( xact_len ) memcpy(_usbd_ctrl_buf, _ctrl_xfer.buffer, xact_len);
96+
if ( xact_len ) {
97+
TU_VERIFY(0 == tu_memcpy_s(_usbd_ctrl_buf, CFG_TUD_ENDPOINT0_SIZE, _ctrl_xfer.buffer, xact_len));
98+
}
9799
}
98100

99101
return usbd_edpt_xfer(rhport, ep_addr, xact_len ? _usbd_ctrl_buf : NULL, xact_len);

src/tusb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ void tu_print_mem(void const *buf, uint32_t count, uint8_t indent)
495495
tu_printf("%04X: ", 16*i/item_per_line);
496496
}
497497

498-
memcpy(&value, buf8, size);
498+
tu_memcpy_s(&value, sizeof(value), buf8, size);
499499
buf8 += size;
500500

501501
tu_printf(" ");

0 commit comments

Comments
 (0)