1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-16 11:28:14 +00:00

trezorhal: fix buffer truncation in usb_hid_read

This commit is contained in:
Jan Pochyla 2017-03-28 00:13:32 +02:00 committed by Pavol Rusnak
parent 8eb5cbbe13
commit c258bfdd90

View File

@ -98,16 +98,19 @@ int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len) {
return -2; // Invalid interface type
}
usb_hid_state_t *state = &usb_ifaces[iface_num].hid;
// Copy maximum possible amount of data and truncate the buffer length
if (len < state->rx_buffer_len) {
return 0; // Not enough data in the read buffer
}
memcpy(buf, state->rx_buffer, state->rx_buffer_len);
len = state->rx_buffer_len;
state->rx_buffer_len = 0;
memcpy(buf, state->rx_buffer, len);
// Clear NAK to indicate we are ready to read more data
usb_ep_clear_nak(&usb_dev_handle, state->ep_out);
return state->rx_buffer_len;
return len;
}
int usb_hid_write(uint8_t iface_num, const uint8_t *buf, uint32_t len) {