Skip to content

Commit ea38115

Browse files
committed
make sure TOTAL_DRIVER_COUNT is not overflow 8-bit
1 parent bb48e6a commit ea38115

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/device/usbd.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,24 @@ enum { BUILTIN_DRIVER_COUNT = TU_ARRAY_SIZE(_usbd_driver) };
317317
tu_static usbd_class_driver_t const * _app_driver = NULL;
318318
tu_static uint8_t _app_driver_count = 0;
319319

320-
#define TOTAL_DRIVER_COUNT (_app_driver_count + BUILTIN_DRIVER_COUNT)
320+
#define TOTAL_DRIVER_COUNT ((uint8_t) (_app_driver_count + BUILTIN_DRIVER_COUNT))
321321

322322
// virtually joins built-in and application drivers together.
323323
// Application is positioned first to allow overwriting built-in ones.
324324
TU_ATTR_ALWAYS_INLINE static inline usbd_class_driver_t const * get_driver(uint8_t drvid) {
325-
usbd_class_driver_t const * driver = NULL;
326-
if ( drvid < _app_driver_count ) {
325+
usbd_class_driver_t const *driver = NULL;
326+
if (drvid < _app_driver_count) {
327327
// Application drivers
328328
driver = &_app_driver[drvid];
329-
} else if ( drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0 ){
329+
} else if (drvid < TOTAL_DRIVER_COUNT && BUILTIN_DRIVER_COUNT > 0) {
330330
driver = &_usbd_driver[drvid - _app_driver_count];
331331
}
332332
return driver;
333333
}
334334

335-
336335
//--------------------------------------------------------------------+
337336
// DCD Event
338337
//--------------------------------------------------------------------+
339-
340338
enum { RHPORT_INVALID = 0xFFu };
341339
tu_static uint8_t _usbd_rhport = RHPORT_INVALID;
342340

@@ -488,6 +486,7 @@ bool tud_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
488486
// Get application driver if available
489487
if (usbd_app_driver_get_cb) {
490488
_app_driver = usbd_app_driver_get_cb(&_app_driver_count);
489+
TU_ASSERT(_app_driver_count + BUILTIN_DRIVER_COUNT <= UINT8_MAX);
491490
}
492491

493492
// Init class drivers

0 commit comments

Comments
 (0)