Skip to content

Commit 9b2eed6

Browse files
committed
shared/tinyusb: Fix interrupt char check for disabled state.
Add check for mp_interrupt_char >= 0 before calling memchr() to avoid false matches when interrupt character detection is disabled (mp_interrupt_char == -1). When mp_interrupt_char is -1, memchr() would search for 0xFF which could cause spurious keyboard interrupts if binary data containing 0xFF is transferred over USB-CDC. Signed-off-by: Andrew Leech <[email protected]>
1 parent 7735584 commit 9b2eed6

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,27 @@ void tud_cdc_rx_cb(uint8_t itf) {
9696
}
9797

9898
#if MICROPY_KBD_EXCEPTION
99-
// Scan for interrupt character
100-
uint8_t *intr_pos = memchr(temp, mp_interrupt_char, got);
101-
if (intr_pos != NULL) {
102-
// Found interrupt character
103-
size_t pre_len = intr_pos - temp;
104-
105-
// Copy bytes before interrupt char (if any)
106-
if (pre_len > 0) {
107-
ringbuf_put_bytes(&stdin_ringbuf, temp, pre_len);
108-
}
99+
// Scan for interrupt character (only if enabled, i.e., >= 0)
100+
if (mp_interrupt_char >= 0) {
101+
uint8_t *intr_pos = memchr(temp, mp_interrupt_char, got);
102+
if (intr_pos != NULL) {
103+
// Found interrupt character
104+
size_t pre_len = intr_pos - temp;
105+
106+
// Copy bytes before interrupt char (if any)
107+
if (pre_len > 0) {
108+
ringbuf_put_bytes(&stdin_ringbuf, temp, pre_len);
109+
}
109110

110-
// Clear the ring buffer
111-
stdin_ringbuf.iget = stdin_ringbuf.iput = 0;
111+
// Clear the ring buffer
112+
stdin_ringbuf.iget = stdin_ringbuf.iput = 0;
112113

113-
// Schedule keyboard interrupt
114-
mp_sched_keyboard_interrupt();
114+
// Schedule keyboard interrupt
115+
mp_sched_keyboard_interrupt();
115116

116-
// Stop processing (discard remaining bytes in temp and USB buffer)
117-
return;
117+
// Stop processing (discard remaining bytes in temp and USB buffer)
118+
return;
119+
}
118120
}
119121
#endif
120122

0 commit comments

Comments
 (0)