Fixed issue with negative modulo with unaligned lookaheads

When the lookahead buffer wraps around in an unaligned filesystem, it's
possible for blocks at the beginning of the disk to have a negative distance
from the lookahead, but still reside in the lookahead buffer.

Switching to signed modulo doesn't quite work due to how negative modulo
is implemented in C, so the simple solution is to shift the region to be
positive.
This commit is contained in:
Christopher Haster
2017-09-17 14:52:25 -05:00
parent 0982020fb3
commit 26dd49aa04
2 changed files with 9 additions and 2 deletions

View File

@@ -9,6 +9,9 @@ script:
-include stdio.h -Werror' make all size -include stdio.h -Werror' make all size
# run tests # run tests
- CFLAGS="-DLFS_READ_SIZE=16 -DLFS_PROG_SIZE=16" make test - make test
# run tests with a few different configurations
- CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test - CFLAGS="-DLFS_READ_SIZE=1 -DLFS_PROG_SIZE=1" make test
- CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test - CFLAGS="-DLFS_READ_SIZE=512 -DLFS_PROG_SIZE=512" make test
- CFLAGS="-DLFS_BLOCK_COUNT=1023" make test

6
lfs.c
View File

@@ -262,7 +262,10 @@ int lfs_deorphan(lfs_t *lfs);
static int lfs_alloc_lookahead(void *p, lfs_block_t block) { static int lfs_alloc_lookahead(void *p, lfs_block_t block) {
lfs_t *lfs = p; lfs_t *lfs = p;
lfs_block_t off = (block - lfs->free.start) % lfs->cfg->block_count; lfs_block_t off = (((lfs_soff_t)(block - lfs->free.start)
% (lfs_soff_t)(lfs->cfg->block_count))
+ lfs->cfg->block_count) % lfs->cfg->block_count;
if (off < lfs->cfg->lookahead) { if (off < lfs->cfg->lookahead) {
lfs->free.lookahead[off / 32] |= 1U << (off % 32); lfs->free.lookahead[off / 32] |= 1U << (off % 32);
} }
@@ -994,6 +997,7 @@ static int lfs_index_extend(lfs_t *lfs,
if (err) { if (err) {
return err; return err;
} }
assert(*block >= 2 && *block <= lfs->cfg->block_count);
err = lfs_bd_erase(lfs, *block); err = lfs_bd_erase(lfs, *block);
if (err) { if (err) {