Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/littlefs-project/littlefs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Pugh <bnp117@gmail.com>2023-08-17 09:07:55 +0300
committerBrian Pugh <bnp117@gmail.com>2023-08-17 09:07:55 +0300
commitdf238ebac6adb7f0111af757e79df104f64d9963 (patch)
tree815d681ee0e7bd90244852f1a731feaa6b2e18e0
parentbe6812213dc48a14ad48db9f7d4352d6120f70d8 (diff)
Add a unit test; currently hanging on final permutation.
Some block-device bound-checks are disabled during superblock search.
-rw-r--r--bd/lfs_emubd.c6
-rw-r--r--lfs.c8
-rw-r--r--tests/test_superblocks.toml15
3 files changed, 22 insertions, 7 deletions
diff --git a/bd/lfs_emubd.c b/bd/lfs_emubd.c
index 2992553..546a90c 100644
--- a/bd/lfs_emubd.c
+++ b/bd/lfs_emubd.c
@@ -237,7 +237,7 @@ int lfs_emubd_read(const struct lfs_config *cfg, lfs_block_t block,
lfs_emubd_t *bd = cfg->context;
// check if read is valid
- LFS_ASSERT(block < cfg->block_count);
+ LFS_ASSERT(!cfg->block_count || block < cfg->block_count);
LFS_ASSERT(off % cfg->read_size == 0);
LFS_ASSERT(size % cfg->read_size == 0);
LFS_ASSERT(off+size <= cfg->block_size);
@@ -287,7 +287,7 @@ int lfs_emubd_prog(const struct lfs_config *cfg, lfs_block_t block,
lfs_emubd_t *bd = cfg->context;
// check if write is valid
- LFS_ASSERT(block < cfg->block_count);
+ LFS_ASSERT(!cfg->block_count || block < cfg->block_count);
LFS_ASSERT(off % cfg->prog_size == 0);
LFS_ASSERT(size % cfg->prog_size == 0);
LFS_ASSERT(off+size <= cfg->block_size);
@@ -376,7 +376,7 @@ int lfs_emubd_erase(const struct lfs_config *cfg, lfs_block_t block) {
lfs_emubd_t *bd = cfg->context;
// check if erase is valid
- LFS_ASSERT(block < cfg->block_count);
+ LFS_ASSERT(!cfg->block_count || block < cfg->block_count);
// get the block
lfs_emubd_block_t *b = lfs_emubd_mutblock(cfg, &bd->blocks[block]);
diff --git a/lfs.c b/lfs.c
index d6588de..2bb7fe7 100644
--- a/lfs.c
+++ b/lfs.c
@@ -46,8 +46,8 @@ static int lfs_bd_read(lfs_t *lfs,
lfs_block_t block, lfs_off_t off,
void *buffer, lfs_size_t size) {
uint8_t *data = buffer;
- if (block >= lfs->block_count ||
- off+size > lfs->cfg->block_size) {
+ if (lfs->block_count &&
+ (block >= lfs->block_count || off+size > lfs->cfg->block_size)) {
return LFS_ERR_CORRUPT;
}
@@ -104,7 +104,7 @@ static int lfs_bd_read(lfs_t *lfs,
}
// load to cache, first condition can no longer fail
- LFS_ASSERT(block < lfs->block_count);
+ LFS_ASSERT(!lfs->block_count || block < lfs->block_count);
rcache->block = block;
rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
rcache->size = lfs_min(
@@ -4389,7 +4389,7 @@ static int lfs_validate_superblock(lfs_t *lfs, lfs_superblock_t *superblock){
lfs->attr_max = superblock->attr_max;
}
- if (superblock->block_count != lfs->cfg->block_count) {
+ if (lfs->cfg->block_count && superblock->block_count != lfs->cfg->block_count) {
LFS_ERROR("Invalid block count (%"PRIu32" != %"PRIu32")",
superblock->block_count, lfs->cfg->block_count);
return LFS_ERR_INVAL;
diff --git a/tests/test_superblocks.toml b/tests/test_superblocks.toml
index 0aff84b..0622a4a 100644
--- a/tests/test_superblocks.toml
+++ b/tests/test_superblocks.toml
@@ -5,6 +5,21 @@ code = '''
lfs_format(&lfs, cfg) => 0;
'''
+# tests formatting from interpretting a previous superblock
+[cases.test_superblocks_format_unknown_block_count]
+code = '''
+ lfs_t lfs;
+ lfs_format(&lfs, cfg) => 0;
+ assert(lfs.block_count == cfg->block_count);
+
+ memset(&lfs, 0, sizeof(lfs));
+ struct lfs_config tweaked_cfg = *cfg;
+ tweaked_cfg.block_count = 0;
+ lfs_format(&lfs, &tweaked_cfg) => 0;
+ assert(lfs.block_count == cfg->block_count);
+'''
+
+
# mount/unmount
[cases.test_superblocks_mount]
code = '''