In the usb_write_bytes and uart_write_bytes routines, a memcpy was previously
used to copy untransmitted bytes to the beginning of the buffer (ud->uart_buffer
and ud->usb_buffer, respectively). Since the source and destination regions of
memory may potentially overlap, the use of memcpy may lead to undefined results.
From the draft C89 standard:
4.11.2.1 The memcpy function
Synopsis
#include <string.h>
void *memcpy(void *s1, const void *s2, size_t n);
Description
The memcpy function copies n characters from the object pointed to
by s2 into the object pointed to by s1 . If copying takes place
between objects that overlap, the behavior is undefined.
Returns
The memcpy function returns the value of s1 .
By using memmove rather than memcpy in the usb_write_bytes and uart_write_bytes
routines, the potential for undefined behavior can be avoided.
tud_cdc_n_write() may not be able to write the full buffer, so we need to
handle that by moving the remaining bytes in the buffer to the buffer start.
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
With these changes, both HW UARTs will be exposed as two independent USB-UART
bridge controllers.
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>