From b0a4a44e5b2e6baeba7255b5abd32fee018b3aa7 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 17 Apr 2023 19:58:09 -0500 Subject: [PATCH] Added explicit assert for minimum block size of 128 bytes There was already an assert for this, but because it included the underlying equation for the requirement it was too confusing for users that had no prior knowledge for why the assert could trigger. The math works out such that 128 bytes is a reasonable minimum requirement, so I've added that number as an explicit assert. Hopefully this makes this sort of situation easier to debug. Note that this requirement would need to be increased to 512 bytes if block addresses are ever increased to 64-bits. DESIGN.md goes into more detail why this is. --- lfs.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lfs.c b/lfs.c index 26280fa8..30452aa5 100644 --- a/lfs.c +++ b/lfs.c @@ -3917,7 +3917,10 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) { LFS_ASSERT(lfs->cfg->cache_size % lfs->cfg->prog_size == 0); LFS_ASSERT(lfs->cfg->block_size % lfs->cfg->cache_size == 0); - // check that the block size is large enough to fit ctz pointers + // check that the block size is large enough to fit all ctz pointers + LFS_ASSERT(lfs->cfg->block_size >= 128); + // this is the exact calculation for all ctz pointers, if this fails + // and the simpler assert above does not, math must be broken LFS_ASSERT(4*lfs_npw2(0xffffffff / (lfs->cfg->block_size-2*4)) <= lfs->cfg->block_size);