Skip to content

Commit a4695d9

Browse files
committed
Avoid out-of-bounds errors caused by invalid endpoint number
1 parent 91c3a4f commit a4695d9

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/device/usbd.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ typedef struct {
143143
tu_static usbd_device_t _usbd_dev;
144144
static volatile uint8_t _usbd_queued_setup;
145145

146+
// Get a validated endpoint number
147+
static uint8_t tud_edpt_number(uint8_t addr) {
148+
uint8_t epnum = tu_edpt_number(addr);
149+
TU_ASSERT(epnum < CFG_TUD_ENDPPOINT_MAX);
150+
return epnum;
151+
}
152+
146153
//--------------------------------------------------------------------+
147154
// Class Driver
148155
//--------------------------------------------------------------------+
@@ -699,7 +706,7 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) {
699706
case DCD_EVENT_XFER_COMPLETE: {
700707
// Invoke the class callback associated with the endpoint address
701708
uint8_t const ep_addr = event.xfer_complete.ep_addr;
702-
uint8_t const epnum = tu_edpt_number(ep_addr);
709+
uint8_t const epnum = tud_edpt_number(ep_addr);
703710
uint8_t const ep_dir = tu_edpt_dir(ep_addr);
704711

705712
TU_LOG_USBD("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
@@ -964,7 +971,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const
964971
//------------- Endpoint Request -------------//
965972
case TUSB_REQ_RCPT_ENDPOINT: {
966973
uint8_t const ep_addr = tu_u16_low(p_request->wIndex);
967-
uint8_t const ep_num = tu_edpt_number(ep_addr);
974+
uint8_t const ep_num = tud_edpt_number(ep_addr);
968975
uint8_t const ep_dir = tu_edpt_dir(ep_addr);
969976

970977
TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) );
@@ -1306,7 +1313,7 @@ TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr)
13061313
case DCD_EVENT_XFER_COMPLETE: {
13071314
// Invoke the class callback associated with the endpoint address
13081315
uint8_t const ep_addr = event->xfer_complete.ep_addr;
1309-
uint8_t const epnum = tu_edpt_number(ep_addr);
1316+
uint8_t const epnum = tud_edpt_number(ep_addr);
13101317
uint8_t const ep_dir = tu_edpt_dir(ep_addr);
13111318

13121319
send = true;
@@ -1413,7 +1420,7 @@ bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr) {
14131420
// TODO add this check later, also make sure we don't starve an out endpoint while suspending
14141421
// TU_VERIFY(tud_ready());
14151422

1416-
uint8_t const epnum = tu_edpt_number(ep_addr);
1423+
uint8_t const epnum = tud_edpt_number(ep_addr);
14171424
uint8_t const dir = tu_edpt_dir(ep_addr);
14181425
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];
14191426

@@ -1423,7 +1430,7 @@ bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr) {
14231430
bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr) {
14241431
(void) rhport;
14251432

1426-
uint8_t const epnum = tu_edpt_number(ep_addr);
1433+
uint8_t const epnum = tud_edpt_number(ep_addr);
14271434
uint8_t const dir = tu_edpt_dir(ep_addr);
14281435
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];
14291436

@@ -1433,7 +1440,7 @@ bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr) {
14331440
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) {
14341441
rhport = _usbd_rhport;
14351442

1436-
uint8_t const epnum = tu_edpt_number(ep_addr);
1443+
uint8_t const epnum = tud_edpt_number(ep_addr);
14371444
uint8_t const dir = tu_edpt_dir(ep_addr);
14381445

14391446
// TODO skip ready() check for now since enumeration also use this API
@@ -1472,7 +1479,7 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t t
14721479
bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t* ff, uint16_t total_bytes) {
14731480
rhport = _usbd_rhport;
14741481

1475-
uint8_t const epnum = tu_edpt_number(ep_addr);
1482+
uint8_t const epnum = tud_edpt_number(ep_addr);
14761483
uint8_t const dir = tu_edpt_dir(ep_addr);
14771484

14781485
TU_LOG_USBD(" Queue ISO EP %02X with %u bytes ... ", ep_addr, total_bytes);
@@ -1500,7 +1507,7 @@ bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t* ff, uint16_
15001507
bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr) {
15011508
(void) rhport;
15021509

1503-
uint8_t const epnum = tu_edpt_number(ep_addr);
1510+
uint8_t const epnum = tud_edpt_number(ep_addr);
15041511
uint8_t const dir = tu_edpt_dir(ep_addr);
15051512

15061513
return _usbd_dev.ep_status[epnum][dir].busy;
@@ -1509,7 +1516,7 @@ bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr) {
15091516
void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr) {
15101517
rhport = _usbd_rhport;
15111518

1512-
uint8_t const epnum = tu_edpt_number(ep_addr);
1519+
uint8_t const epnum = tud_edpt_number(ep_addr);
15131520
uint8_t const dir = tu_edpt_dir(ep_addr);
15141521

15151522
// only stalled if currently cleared
@@ -1522,7 +1529,7 @@ void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr) {
15221529
void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) {
15231530
rhport = _usbd_rhport;
15241531

1525-
uint8_t const epnum = tu_edpt_number(ep_addr);
1532+
uint8_t const epnum = tud_edpt_number(ep_addr);
15261533
uint8_t const dir = tu_edpt_dir(ep_addr);
15271534

15281535
// only clear if currently stalled
@@ -1535,7 +1542,7 @@ void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) {
15351542
bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr) {
15361543
(void) rhport;
15371544

1538-
uint8_t const epnum = tu_edpt_number(ep_addr);
1545+
uint8_t const epnum = tud_edpt_number(ep_addr);
15391546
uint8_t const dir = tu_edpt_dir(ep_addr);
15401547

15411548
return _usbd_dev.ep_status[epnum][dir].stalled;
@@ -1554,7 +1561,7 @@ void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr) {
15541561

15551562
TU_LOG_USBD(" CLOSING Endpoint: 0x%02X\r\n", ep_addr);
15561563

1557-
uint8_t const epnum = tu_edpt_number(ep_addr);
1564+
uint8_t const epnum = tud_edpt_number(ep_addr);
15581565
uint8_t const dir = tu_edpt_dir(ep_addr);
15591566

15601567
dcd_edpt_close(rhport, ep_addr);
@@ -1599,7 +1606,7 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const* desc_ep)
15991606
#ifdef TUP_DCD_EDPT_ISO_ALLOC
16001607
rhport = _usbd_rhport;
16011608

1602-
uint8_t const epnum = tu_edpt_number(desc_ep->bEndpointAddress);
1609+
uint8_t const epnum = tud_edpt_number(desc_ep->bEndpointAddress);
16031610
uint8_t const dir = tu_edpt_dir(desc_ep->bEndpointAddress);
16041611

16051612
TU_ASSERT(epnum < CFG_TUD_ENDPPOINT_MAX);

0 commit comments

Comments
 (0)