From ea431bd6aefd6f4f2a00b1b90bb15bcb0c9a411d Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Fri, 4 Oct 2024 13:29:03 -0500 Subject: [PATCH] Added some checks that metadata_max makes sense Like the read/prog/block_size checks, these are just asserts. If these invariants are broken the filesystem will break in surprising ways. --- lfs.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lfs.c b/lfs.c index 1f8c9c65..40daf759 100644 --- a/lfs.c +++ b/lfs.c @@ -4210,6 +4210,15 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) { LFS_ASSERT(lfs->cfg->compact_thresh == (lfs_size_t)-1 || lfs->cfg->compact_thresh <= lfs->cfg->block_size); + // check that metadata_max is a multiple of read_size and prog_size, + // and a factor of the block_size + LFS_ASSERT(!lfs->cfg->metadata_max + || lfs->cfg->metadata_max % lfs->cfg->read_size == 0); + LFS_ASSERT(!lfs->cfg->metadata_max + || lfs->cfg->metadata_max % lfs->cfg->prog_size == 0); + LFS_ASSERT(!lfs->cfg->metadata_max + || lfs->cfg->block_size % lfs->cfg->metadata_max == 0); + // setup read cache if (lfs->cfg->read_buffer) { lfs->rcache.buffer = lfs->cfg->read_buffer;