drivers: serial: uart_async_rx: Fix race condition

There was a wrong order of conditions checking. First indexes were
compared and then completed flag was checked. It was possible that
after checking first condition context is preempted and new data
is written to the buffer and completed flag is set. In that case
both conditions are met but data added in preemption is dropped.
In order to avoid that completed flag must be checked first.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2024-02-12 11:18:10 +01:00 committed by Anas Nashif
parent d2bd82eb5f
commit 17dc3da35d

View File

@ -91,7 +91,7 @@ size_t uart_async_rx_data_claim(struct uart_async_rx *rx_data, uint8_t **data, s
/* Even though buffer is released in consume phase it is possible that
* it is required here as well (e.g. was not completed previously).
*/
if ((buf->rd_idx == buf->wr_idx) && (buf->completed == 1)) {
if ((buf->completed == 1) && (rx_data->rd_idx == buf->wr_idx)) {
usr_rx_buf_release(rx_data, buf);
} else {
break;
@ -110,7 +110,7 @@ bool uart_async_rx_data_consume(struct uart_async_rx *rx_data, size_t length)
buf->rd_idx += length;
/* Attempt to release the buffer if it is completed and all data is consumed. */
if ((buf->rd_idx == buf->wr_idx) && (buf->completed == 1)) {
if ((buf->completed == 1) && (rx_data->rd_idx == buf->wr_idx)) {
usr_rx_buf_release(rx_data, buf);
}