From b571846ea4e69981dc593faa82c861cdd6ece078 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Thu, 8 Apr 2021 17:08:28 -0700 Subject: [PATCH] cbfs: mcache: Fix size calculation for perfectly full cache cbfs_mcache_real_size() has a subtle flaw: when the cache is perfectly full to the end (so that the termination token sits exactly at the end of the available space), the loop counting the size ends prematurely. This means that when migrating the cache to CBMEM the terminating token is not copied, which isn't actually noticeable unless you're looking for a file that's not in the cache (because it doesn't exist or because not all files fit when building). This patch fixes the problem and slightly changes the error message for when a cache isn't terminated (to make it more clear that this is a different condition from a "normal" cache overflow that can happen when building if there's not enough room to fit all files). Signed-off-by: Julius Werner Change-Id: I8d89e7dadc958f97b173b3a2352f2010c8a3d1d5 Reviewed-on: https://review.coreboot.org/c/coreboot/+/52200 Tested-by: build bot (Jenkins) Reviewed-by: Aaron Durbin --- src/commonlib/bsd/cbfs_mcache.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commonlib/bsd/cbfs_mcache.c b/src/commonlib/bsd/cbfs_mcache.c index 0d5d8e0221..965f9faef6 100644 --- a/src/commonlib/bsd/cbfs_mcache.c +++ b/src/commonlib/bsd/cbfs_mcache.c @@ -118,7 +118,7 @@ cb_err_t cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char * current += ALIGN_UP(data_offset, CBFS_MCACHE_ALIGNMENT); } - ERROR("CBFS mcache overflow!\n"); + ERROR("CBFS mcache is not terminated!\n"); /* should never happen */ return CB_ERR; } @@ -127,7 +127,7 @@ size_t cbfs_mcache_real_size(const void *mcache, size_t mcache_size) const void *end = mcache + mcache_size; const void *current = mcache; - while (current + sizeof(uint32_t) < end) { + while (current + sizeof(uint32_t) <= end) { const union mcache_entry *entry = current; if (entry->magic == MCACHE_MAGIC_FULL || entry->magic == MCACHE_MAGIC_END) {