From af515ef87bf63d48d67fee8155f5d904baff9b84 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Sat, 4 May 2024 14:58:43 +1000 Subject: [PATCH] logging: log_cache: ensure cache contains an entry Return an error if the provided cache configuration is not large enough to hold a single entry. An empty `active` and `idle` list causes NULL dereferences in `log_cache_get`. Signed-off-by: Jordan Yates --- subsys/logging/log_cache.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subsys/logging/log_cache.c b/subsys/logging/log_cache.c index 5bc51f3635b..4899a3435d9 100644 --- a/subsys/logging/log_cache.c +++ b/subsys/logging/log_cache.c @@ -4,6 +4,8 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include + #include "log_cache.h" #define LOG_CACHE_DBG 0 @@ -29,6 +31,11 @@ int log_cache_init(struct log_cache *cache, const struct log_cache_config *confi uint32_t entry_cnt = config->buf_len / entry_size; struct log_cache_entry *entry = config->buf; + /* Ensure the cache has at least one entry */ + if (entry_cnt == 0) { + return -EINVAL; + } + /* Add all entries to idle list */ for (uint32_t i = 0; i < entry_cnt; i++) { sys_slist_append(&cache->idle, &entry->node);