Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usb_send does not take into account usb_endpoints[ep_num].tx_size #52

Closed
r2axz opened this issue Feb 11, 2022 · 0 comments · Fixed by #53
Closed

usb_send does not take into account usb_endpoints[ep_num].tx_size #52

r2axz opened this issue Feb 11, 2022 · 0 comments · Fixed by #53
Assignees
Labels
bug Something isn't working

Comments

@r2axz
Copy link
Owner

r2axz commented Feb 11, 2022

size_t usb_send(uint8_t ep_num, const void *buf, size_t count) {
    ep_reg_t *ep_reg = ep_regs(ep_num);
    usb_pbuffer_data_t *ep_buf = (usb_pbuffer_data_t *)(USB_PMAADDR + (usb_btable[ep_num].tx_offset<<1));
    pb_word_t *buf_p = (pb_word_t*)buf;
    pb_word_t words_left  = count >> 1;
    size_t tx_space_available = usb_endpoints[ep_num].tx_size;
    if (count > tx_space_available) {
        count = tx_space_available;
    }
    while (words_left--) {
        (ep_buf++)->data = *buf_p++;
    }
    if (count & 0x01) {
        (ep_buf)->data = (uint8_t)*buf_p;
    }
    usb_btable[ep_num].tx_count = count;
    *ep_reg = ((*ep_reg ^ USB_EP_TX_VALID) & (USB_EPREG_MASK | USB_EPTX_STAT)) | (USB_EP_CTR_RX | USB_EP_CTR_TX);
    return count;
}

words_left is initialized before count > tx_space_available check, which leads to writing past the ep tx buffer. Surprisingly, this does not cause any major problems because usb_send is mostly used during the device USB enumeration phase and its circular buffer sibling (usb_circ_buf_send) does not have this issue.

@r2axz r2axz added the bug Something isn't working label Feb 11, 2022
@r2axz r2axz self-assigned this Feb 11, 2022
@r2axz r2axz linked a pull request Feb 11, 2022 that will close this issue
@r2axz r2axz closed this as completed in #53 Feb 11, 2022
r2axz added a commit that referenced this issue Feb 11, 2022
FIX: usb_send tx_space_available check, closes #52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant