From ee1e517ccfa0554cde4985e990fb7fa8751ae308 Mon Sep 17 00:00:00 2001 From: Bjarki Arge Andreasen Date: Mon, 26 Aug 2024 11:48:48 +0200 Subject: [PATCH] ipc: icmsg: configure PBUF RX rx_buffer using kconfig The size of the rx_buffer used by icmsg.c mbox_callback_process() is not MISRA compliant, being "dynamically" allocated on the stack. This commit adds a kconfig to set the size of the read buffer and asserts that it is large enough to read all received data. Signed-off-by: Bjarki Arge Andreasen --- subsys/ipc/ipc_service/lib/Kconfig.icmsg | 8 ++++++++ subsys/ipc/ipc_service/lib/icmsg.c | 9 +++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/subsys/ipc/ipc_service/lib/Kconfig.icmsg b/subsys/ipc/ipc_service/lib/Kconfig.icmsg index 7a9a93e86de..93427404dca 100644 --- a/subsys/ipc/ipc_service/lib/Kconfig.icmsg +++ b/subsys/ipc/ipc_service/lib/Kconfig.icmsg @@ -80,3 +80,11 @@ config PBUF with read/write semantics on top of a memory region shared by the reader and writer. It optionally embeds cache and memory barrier management to ensure correct data access. + +if PBUF + +config PBUF_RX_READ_BUF_SIZE + int "Size of PBUF read buffer in bytes" + default 128 + +endif # PBUF diff --git a/subsys/ipc/ipc_service/lib/icmsg.c b/subsys/ipc/ipc_service/lib/icmsg.c index 73fac78fd69..e73bdaaa723 100644 --- a/subsys/ipc/ipc_service/lib/icmsg.c +++ b/subsys/ipc/ipc_service/lib/icmsg.c @@ -176,6 +176,7 @@ static void mbox_callback_process(struct icmsg_data_t *dev_data) #ifdef CONFIG_MULTITHREADING struct icmsg_data_t *dev_data = CONTAINER_OF(item, struct icmsg_data_t, mbox_work); #endif + uint8_t rx_buffer[CONFIG_PBUF_RX_READ_BUF_SIZE] __aligned(4); atomic_t state = atomic_get(&dev_data->state); @@ -186,9 +187,13 @@ static void mbox_callback_process(struct icmsg_data_t *dev_data) return; } - uint8_t rx_buffer[len]; + __ASSERT_NO_MSG(len <= sizeof(rx_buffer)); - len = pbuf_read(dev_data->rx_pb, rx_buffer, len); + if (sizeof(rx_buffer) < len) { + return; + } + + len = pbuf_read(dev_data->rx_pb, rx_buffer, sizeof(rx_buffer)); if (state == ICMSG_STATE_READY) { if (dev_data->cb->received) {