From 9ea9bed77b18fa17ea59f41c6bfdcbfdb0926749 Mon Sep 17 00:00:00 2001 From: Richard Acayan Date: Mon, 4 Dec 2023 19:35:19 -0500 Subject: [PATCH] fastrpc: hexagonrpcd: listener: do not attempt to allocate zero outbufs The malloc(3p) manual page states that an implementation may choose to return a non-NULL pointer when the size is zero. This function assumes that there is an element in the array if the pointer is not NULL. When the size is zero, skip the allocation and return NULL. --- fastrpc/hexagonrpcd/listener.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fastrpc/hexagonrpcd/listener.c b/fastrpc/hexagonrpcd/listener.c index d746146..bcdc857 100644 --- a/fastrpc/hexagonrpcd/listener.c +++ b/fastrpc/hexagonrpcd/listener.c @@ -73,6 +73,14 @@ static struct fastrpc_io_buffer *allocate_outbufs(const struct fastrpc_function_ off_t off; uint32_t *sizes; + /* + * POSIX allows malloc to return a non-NULL pointer to a zero-size area + * in memory. Since the code below assumes non-zero size if the pointer + * is non-NULL, exit early if we do not need to allocate anything. + */ + if (out_count == 0) + return NULL; + out_count = def->out_bufs + (def->out_nums && 1); out = malloc(sizeof(struct fastrpc_io_buffer) * out_count); if (out == NULL)