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

drivers: serial: fix potential overflow in fifo_fill and fifo_read #80601

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drivers/serial/leuart_gecko.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static int leuart_gecko_fifo_fill(const struct device *dev,
int len)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(base->STATUS & LEUART_STATUS_TXBL)) {
Expand All @@ -116,7 +116,7 @@ static int leuart_gecko_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
LEUART_TypeDef *base = DEV_BASE(dev);
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(base->STATUS & LEUART_STATUS_RXDATAV)) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_gecko.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static int uart_gecko_fifo_fill(const struct device *dev, const uint8_t *tx_data
int len)
{
const struct uart_gecko_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(config->base->STATUS & USART_STATUS_TXBL)) {
Expand All @@ -237,7 +237,7 @@ static int uart_gecko_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct uart_gecko_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(config->base->STATUS & USART_STATUS_RXDATAV)) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_mcux.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ static int uart_mcux_fifo_fill(const struct device *dev,
int len)
{
const struct uart_mcux_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(UART_GetStatusFlags(config->base) & kUART_TxDataRegEmptyFlag)) {
Expand All @@ -194,7 +194,7 @@ static int uart_mcux_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct uart_mcux_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(UART_GetStatusFlags(config->base) & kUART_RxDataRegFullFlag)) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_mcux_flexcomm.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ static int mcux_flexcomm_fifo_fill(const struct device *dev,
int len)
{
const struct mcux_flexcomm_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(USART_GetStatusFlags(config->base)
Expand All @@ -162,7 +162,7 @@ static int mcux_flexcomm_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct mcux_flexcomm_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(USART_GetStatusFlags(config->base)
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_mcux_iuart.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static int mcux_iuart_fifo_fill(const struct device *dev,
int len)
{
const struct mcux_iuart_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(UART_GetStatusFlag(config->base, kUART_TxEmptyFlag))) {
Expand All @@ -101,7 +101,7 @@ static int mcux_iuart_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct mcux_iuart_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(UART_GetStatusFlag(config->base, kUART_RxDataReadyFlag))) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_mcux_lpsci.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static int mcux_lpsci_fifo_fill(const struct device *dev,
int len)
{
const struct mcux_lpsci_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(LPSCI_GetStatusFlags(config->base)
Expand All @@ -105,7 +105,7 @@ static int mcux_lpsci_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct mcux_lpsci_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(LPSCI_GetStatusFlags(config->base)
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_mcux_lpuart.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ static int mcux_lpuart_fifo_fill(const struct device *dev,
int len)
{
const struct mcux_lpuart_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(LPUART_GetStatusFlags(config->base)
Expand All @@ -248,7 +248,7 @@ static int mcux_lpuart_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct mcux_lpuart_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(LPUART_GetStatusFlags(config->base)
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_nrfx_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ static int uart_nrfx_fifo_fill(const struct device *dev,
const uint8_t *tx_data,
int len)
{
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
event_txdrdy_check()) {
Expand All @@ -806,7 +806,7 @@ static int uart_nrfx_fifo_read(const struct device *dev,
uint8_t *rx_data,
const int size)
{
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((size - num_rx > 0) &&
nrf_uart_event_check(uart0_addr, NRF_UART_EVENT_RXDRDY)) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_pl011.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ static int pl011_runtime_config_get(const struct device *dev,
static int pl011_fifo_fill(const struct device *dev,
const uint8_t *tx_data, int len)
{
uint8_t num_tx = 0U;
int num_tx = 0U;

while (!(get_uart(dev)->fr & PL011_FR_TXFF) && (len - num_tx > 0)) {
get_uart(dev)->dr = tx_data[num_tx++];
Expand All @@ -328,7 +328,7 @@ static int pl011_fifo_fill(const struct device *dev,
static int pl011_fifo_read(const struct device *dev,
uint8_t *rx_data, const int len)
{
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) && !(get_uart(dev)->fr & PL011_FR_RXFE)) {
rx_data[num_rx++] = get_uart(dev)->dr;
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_renesas_ra8_sci_b.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static int uart_ra_sci_b_fifo_fill(const struct device *dev, const uint8_t *tx_d
{
struct uart_ra_sci_b_data *data = dev->data;
const struct uart_ra_sci_b_config *cfg = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

if (IS_ENABLED(CONFIG_UART_RA_SCI_B_UART_FIFO_ENABLE) && data->sci.fifo_depth > 0) {
while ((size - num_tx > 0) && cfg->regs->FTSR != 0x10U) {
Expand All @@ -281,7 +281,7 @@ static int uart_ra_sci_b_fifo_read(const struct device *dev, uint8_t *rx_data, c
{
struct uart_ra_sci_b_data *data = dev->data;
const struct uart_ra_sci_b_config *cfg = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

if (IS_ENABLED(CONFIG_UART_RA_SCI_B_UART_FIFO_ENABLE) && data->sci.fifo_depth > 0) {
while ((size - num_rx > 0) && cfg->regs->FRSR_b.R > 0U) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_renesas_ra_sci.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static int uart_ra_sci_fifo_fill(const struct device *dev, const uint8_t *tx_dat
{
struct uart_ra_sci_data *data = dev->data;
const struct uart_ra_sci_config *cfg = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

#if CONFIG_UART_RA_SCI_UART_FIFO_ENABLE
if (data->sci.fifo_depth != 0) {
Expand All @@ -326,7 +326,7 @@ static int uart_ra_sci_fifo_read(const struct device *dev, uint8_t *rx_data, con
{
struct uart_ra_sci_data *data = dev->data;
const struct uart_ra_sci_config *cfg = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

#if CONFIG_UART_RA_SCI_UART_FIFO_ENABLE
if (data->sci.fifo_depth != 0) {
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_rv32m1_lpuart.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static int rv32m1_lpuart_fifo_fill(const struct device *dev,
int len)
{
const struct rv32m1_lpuart_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
(LPUART_GetStatusFlags(config->base)
Expand All @@ -109,7 +109,7 @@ static int rv32m1_lpuart_fifo_read(const struct device *dev, uint8_t *rx_data,
const int len)
{
const struct rv32m1_lpuart_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((len - num_rx > 0) &&
(LPUART_GetStatusFlags(config->base)
Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/uart_stellaris.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ static int uart_stellaris_fifo_fill(const struct device *dev,
int len)
{
const struct uart_stellaris_config *config = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) && ((config->uart->fr & UARTFR_TXFF) == 0U)) {
config->uart->dr = (uint32_t)tx_data[num_tx++];
Expand All @@ -336,7 +336,7 @@ static int uart_stellaris_fifo_read(const struct device *dev,
const int size)
{
const struct uart_stellaris_config *config = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((size - num_rx > 0) && ((config->uart->fr & UARTFR_RXFE) == 0U)) {
rx_data[num_rx++] = (uint8_t)config->uart->dr;
Expand Down
22 changes: 8 additions & 14 deletions drivers/serial/uart_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,14 @@ static inline void __uart_stm32_get_clock(const struct device *dev)

#ifdef CONFIG_UART_INTERRUPT_DRIVEN

typedef void (*fifo_fill_fn)(USART_TypeDef *usart, const void *tx_data,
const uint8_t offset);
typedef void (*fifo_fill_fn)(USART_TypeDef *usart, const void *tx_data, const int offset);

static int uart_stm32_fifo_fill_visitor(const struct device *dev, const void *tx_data, int size,
fifo_fill_fn fill_fn)
{
erwango marked this conversation as resolved.
Show resolved Hide resolved
const struct uart_stm32_config *config = dev->config;
USART_TypeDef *usart = config->usart;
uint8_t num_tx = 0U;
int num_tx = 0U;
unsigned int key;

if (!LL_USART_IsActiveFlag_TXE(usart)) {
Expand All @@ -847,8 +846,7 @@ static int uart_stm32_fifo_fill_visitor(const struct device *dev, const void *tx
return num_tx;
}

static void fifo_fill_with_u8(USART_TypeDef *usart,
const void *tx_data, const uint8_t offset)
static void fifo_fill_with_u8(USART_TypeDef *usart, const void *tx_data, const int offset)
{
const uint8_t *data = (const uint8_t *)tx_data;
/* Send a character (8bit) */
Expand All @@ -865,15 +863,14 @@ static int uart_stm32_fifo_fill(const struct device *dev, const uint8_t *tx_data
fifo_fill_with_u8);
}

typedef void (*fifo_read_fn)(USART_TypeDef *usart, void *rx_data,
const uint8_t offset);
typedef void (*fifo_read_fn)(USART_TypeDef *usart, void *rx_data, const int offset);

static int uart_stm32_fifo_read_visitor(const struct device *dev, void *rx_data, const int size,
fifo_read_fn read_fn)
{
erwango marked this conversation as resolved.
Show resolved Hide resolved
const struct uart_stm32_config *config = dev->config;
USART_TypeDef *usart = config->usart;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((size - num_rx > 0) && LL_USART_IsActiveFlag_RXNE(usart)) {
/* RXNE flag will be cleared upon read from DR|RDR register */
Expand All @@ -894,8 +891,7 @@ static int uart_stm32_fifo_read_visitor(const struct device *dev, void *rx_data,
return num_rx;
}

static void fifo_read_with_u8(USART_TypeDef *usart, void *rx_data,
const uint8_t offset)
static void fifo_read_with_u8(USART_TypeDef *usart, void *rx_data, const int offset)
{
uint8_t *data = (uint8_t *)rx_data;

Expand All @@ -914,8 +910,7 @@ static int uart_stm32_fifo_read(const struct device *dev, uint8_t *rx_data, cons

#ifdef CONFIG_UART_WIDE_DATA

static void fifo_fill_with_u16(USART_TypeDef *usart,
const void *tx_data, const uint8_t offset)
static void fifo_fill_with_u16(USART_TypeDef *usart, const void *tx_data, const int offset)
{
const uint16_t *data = (const uint16_t *)tx_data;

Expand All @@ -933,8 +928,7 @@ static int uart_stm32_fifo_fill_u16(const struct device *dev, const uint16_t *tx
fifo_fill_with_u16);
}

static void fifo_read_with_u16(USART_TypeDef *usart, void *rx_data,
const uint8_t offset)
static void fifo_read_with_u16(USART_TypeDef *usart, void *rx_data, const int offset)
{
uint16_t *data = (uint16_t *)rx_data;

Expand Down
4 changes: 2 additions & 2 deletions drivers/serial/usart_gd32.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int usart_gd32_fifo_fill(const struct device *dev, const uint8_t *tx_data,
int len)
{
const struct gd32_usart_config *const cfg = dev->config;
uint8_t num_tx = 0U;
int num_tx = 0U;

while ((len - num_tx > 0) &&
usart_flag_get(cfg->reg, USART_FLAG_TBE)) {
Expand All @@ -181,7 +181,7 @@ int usart_gd32_fifo_read(const struct device *dev, uint8_t *rx_data,
const int size)
{
const struct gd32_usart_config *const cfg = dev->config;
uint8_t num_rx = 0U;
int num_rx = 0U;

while ((size - num_rx > 0) &&
usart_flag_get(cfg->reg, USART_FLAG_RBNE)) {
Expand Down
Loading